using UnityEngine; using Unity.Netcode; using TMPro; [RequireComponent(typeof(NetworkObject))] public class FoxMovement : NetworkBehaviour { private CharacterController controller; public float Speed = 10f; public float RotateSpeed = 1f; public float Gravity = -9.8f; private Vector3 Velocity = Vector3.zero; public Transform GroundCheck; public float checkRadius = 0.2f; public bool IsGround; public LayerMask layerMask; public NetworkVariable Position = new NetworkVariable(); public NetworkVariable MoveVector = new NetworkVariable(); public NetworkVariable Angle = new NetworkVariable(); public TextMeshPro IsOwnerText; private CharacterController characterController; private void Awake() { characterController = GetComponent(); } // Start is called before the first frame update void Start() { controller = transform.GetComponent(); } // Update is called once per frame void Update() { IsOwnerText.text = IsOwner ? "Owner" : ""; IsOwnerText.text += IsClient ? " Client" : ""; if (IsOwner && IsClient) { ClientInput(); } ClientMoveAndRotate(); } void ClientInput() { var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical"); var move = transform.forward * Speed * vertical * Time.deltaTime; IsGround = Physics.CheckSphere(GroundCheck.position, checkRadius, layerMask); if (IsGround && Velocity.y < 0) { Velocity.y = 0; } else { Velocity.y += Gravity * Time.deltaTime; } updateClientPositionAndRotationServerRpc(move + Velocity * Time.deltaTime, horizontal * RotateSpeed); } void ClientMoveAndRotate() { characterController.Move(MoveVector.Value); transform.Rotate(Vector3.up, Angle.Value); } [ServerRpc] void updateClientPositionAndRotationServerRpc(Vector3 moveVector, float angle) { MoveVector.Value = moveVector; Angle.Value = angle; } }