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();
// 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!