Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug Character Controller movement.

Discussion in 'Netcode for GameObjects' started by axelotl2, Aug 16, 2023.

  1. axelotl2

    axelotl2

    Joined:
    Apr 18, 2023
    Posts:
    2
    Hello, I am currently developing a small game and am currently stuck on the movement, the client controls the owner and the owner controls the client, I have no clue what to do about it, can anyone help?

    My code:

    using Unity.Netcode;
    using Unity.Netcode.Components;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [RequireComponent(typeof(CharacterController))]
    public class NetcodeMovement : NetworkBehaviour
    {
    public float walkingSpeed = 4f;
    public float runningSpeed = 7.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Camera playerCamera;
    public float lookSpeed = 0.1f;
    public float lookXLimit = 90f;

    public Transform neckBone; // Reference to the neck bone transform
    public float neckRotationSpeed = 1.0f; // Adjust the speed of neck rotation

    private CharacterController characterController;
    private Vector3 moveDirection = Vector3.zero;
    private float rotationX = 0;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
    characterController = GetComponent<CharacterController>();

    // Lock cursor
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
    }

    public override void OnNetworkSpawn()
    {
    if (!IsOwner)
    {
    characterController.enabled = false;
    }
    base.OnNetworkSpawn();
    }

    void Update()
    {
    if(!IsOwner) return;
    // We are grounded, so recalculate move direction based on axes
    Vector3 forward = transform.TransformDirection(Vector3.forward);
    Vector3 right = transform.TransformDirection(Vector3.right);
    // Press Left Shift to run
    bool isRunning = Input.GetKey(KeyCode.LeftShift);
    float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
    float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
    float movementDirectionY = moveDirection.y;
    moveDirection = (forward * curSpeedX) + (right * curSpeedY);

    if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
    {
    moveDirection.y = jumpSpeed;
    }
    else
    {
    moveDirection.y = movementDirectionY;
    }

    // Apply gravity
    if (!characterController.isGrounded)
    {
    moveDirection.y -= gravity * Time.deltaTime;
    }

    // Move the controller
    characterController.Move(moveDirection * Time.deltaTime);

    // Player and Camera rotation
    if (canMove)
    {
    rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
    rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);

    // Rotate the neck bone for head movement
    if (neckBone != null)
    {
    float neckRotationX = -Input.GetAxis("Mouse Y") * lookSpeed * neckRotationSpeed;
    neckBone.Rotate(neckRotationX, 0, 0);

    // Synchronize neck rotation across network
    UpdateNeckRotationClientRpc(neckBone.localRotation.eulerAngles.x);
    }

    playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
    transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
    }
    }

    [ClientRpc]
    private void UpdateNeckRotationClientRpc(float newRotationX)
    {
    if (neckBone != null)
    {
    neckBone.localRotation = Quaternion.Euler(newRotationX, 0, 0);
    }
    }
    }

    I'm also using a client network transform.

    Thanks!
     
  2. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    98
    Are you sure it's a controller issue? Could the camera system be accidentally latching the camera onto the wrong character?
     
  3. axelotl2

    axelotl2

    Joined:
    Apr 18, 2023
    Posts:
    2
    Yes, I am using a character controller, about the camera, I don't actually know, it probably is, but I don't know how to fix it.