How to make a Client Network Rigidbody

Hi.
I am trying to make a simple Football game where all players can obviously move the ball. I am version 1.1.0 of Netcode for Gameobjects.

The problem is, I attached a ClientNetworkTransform, a NetworkRigidbody, and a NetworkObject to the ball. This should in theory allow any player to move the ball. But it doesnt.
The host can move the ball freely, but any client cannot. Using debug mode I can see that the NetworkRigidbody (Script from the Netcode library) has indeed the “IsServerAuthoritative” in false.

So, why is that this configuration doesnt allow clients to move the rigidbody in the ball?

The player has a rigidbody and the movement script applies a force to the player. And then that movement ends up generating a collision with the ball, moving it also.

Screenshots:


PlayerMovement code:

using Unity.Netcode;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : NetworkBehaviour
{
 
    [SerializeField] private float _acceleration = 80;
    [SerializeField] private float _maxVelocity = 10;
    private Vector3 _input;
    private Rigidbody _rb;
 
    [SerializeField] private float _rotationSpeed = 450;
    private Plane _groundPlane = new(Vector3.up, Vector3.zero);
    private Camera _cam;

    private void Awake()
    {
        _rb = GetComponent<Rigidbody>();
        _cam = Camera.main;
    }

    private void Update()
    {
        _input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    }

    private void FixedUpdate()
    {
        HandleMovement();
        //HandleRotation();
    }

    #region Movement

    private void HandleMovement()
    {
        _rb.velocity += _input.normalized * (_acceleration * Time.deltaTime);
        _rb.velocity = Vector3.ClampMagnitude(_rb.velocity, _maxVelocity);
    }

    #endregion

    #region Rotation
 
    private void HandleRotation()
    {
        Ray ray = _cam.ScreenPointToRay(Input.mousePosition);

        if (_groundPlane.Raycast(ray, out float enter))
        {
            Vector3 hitPoint = ray.GetPoint(enter);

            Vector3 dir = hitPoint - transform.position;
            Quaternion rot = Quaternion.LookRotation(dir);

            transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, _rotationSpeed * Time.deltaTime);
        }
    }
 
    #endregion

Thanks for the help!

1 Like

Privacy & Terms