Search Unity

Question Mirror Rigidbody Movement

Discussion in 'Unity Hub' started by Hansibaba, Apr 4, 2023.

  1. Hansibaba

    Hansibaba

    Joined:
    Aug 13, 2021
    Posts:
    1
    I am creating a game that utilizes mirror for the networking and I am trying to familiarize myself with mirror by creating a small project in which players can join and move around. I'm trying to use rigidbodies for the movement although right now my code isn't allowing me to jump and gravity is not working either. Here is the script for the player controller. The player consists of a cylinder with the network identity, network transform (reliable), rigidbody, and movement components added. Any help would be appreciated, thanks.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class Movement : NetworkBehaviour
    7. {
    8.  
    9.     [SerializeField] Rigidbody rb;
    10.     public float moveForce = 0.001f;
    11.     public float jumpForce = 200f;
    12.     public Vector3 MoveVector = new Vector3(0, 0, 0);
    13.  
    14.  
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.      
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.  
    26.         if (!Application.isFocused) return;
    27.  
    28.         // movement for local player
    29.         if (isLocalPlayer)
    30.         {
    31.             float h = Input.GetAxisRaw("Horizontal");
    32.             float v = Input.GetAxisRaw("Vertical");
    33.  
    34.             MoveVector = new Vector3(h, 0, v);
    35.  
    36.             if (Input.GetKeyDown(KeyCode.Space))
    37.             {
    38.                 rb.AddForce(Vector3.up * jumpForce);
    39.             }
    40.  
    41.             rb.MovePosition((MoveVector * moveForce * Time.deltaTime) + transform.position);
    42.         }
    43.     }
    44. }
    45.