Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

NetworkingTransform detects rotation but not movement #471

Closed samanake closed 3 years ago

samanake commented 3 years ago

I'm using Project Cloner to test the interaction between Host and Client/Server and Client. I use clicks to move the CharacterController like in popular MOBAs. Rotations and animations(using Networking Animation) is fine, but translational movements don't register between clients. I can't figure out how rotations are fine but not movements. Could it possibly be some of the functions I'm using?

Environment (please complete the following information):


using System.Collections.Generic;
using UnityEngine;
using MLAPI;

public class animationStateController : NetworkedBehaviour
{
    Animator animator;
    CharacterController controller;
    int isWalkingHash;
    public float speed = .1F;
    public float rotateSpeed = 0.2F;
    public float walkRange = .5F;

    Vector3 newPosition;
    void Start()
    {
        if (IsLocalPlayer)
        {
            animator = GetComponent<Animator>();
            controller = GetComponent<CharacterController>();
            isWalkingHash = Animator.StringToHash("isWalking");
            var camera = GameObject.FindGameObjectWithTag("MainCamera");
            camera.GetComponent<CameraMovement>().player = transform;
            newPosition = transform.position;
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (IsLocalPlayer)
        {
            if (Input.GetKey("mouse 0"))
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Ground"))
                {
                    newPosition = hit.point;
                }
                Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
                Debug.DrawLine(transform.position,newPosition);
            }

            MovePlayer();

        }
    }

    void MovePlayer()
    {
        if (Vector3.Distance(newPosition,transform.position) > walkRange)
        {
            //controller.Move(new Vector3(0.1f,0.1f,0.1f));
            controller.transform.position = Vector3.MoveTowards(controller.transform.position, newPosition, speed);
            animator.SetBool(isWalkingHash, true);
            RotatePlayer();
        }
        else
            animator.SetBool(isWalkingHash, false);
    }

    void RotatePlayer()
    {
        Quaternion newRotation = Quaternion.LookRotation(newPosition - controller.transform.position, Vector3.up);
        controller.transform.rotation = Quaternion.Slerp(controller.transform.rotation, newRotation, rotateSpeed);
    }
}```
LukeStampfli commented 3 years ago

This is most likely because you have a CharacterController on this movement script. A CharacterController overides all position changes done to the transform including the ones from NetworkedTransform.

Try to disable the CharacterController if is LocalPlayer is false and see if that fixes the issue.

samanake commented 3 years ago

Thanks. That worked. How does the CharacterController override all position changes? Is this under the hood somewhere?

LukeStampfli commented 3 years ago

I'm not sure how exactly the CharacterController does this but it stores its own position internally and then reapplies this to the transform each update.

I'll close this issue for visibility but feel free to continue chatting or to reopen the issue if new questions arrive.