Search Unity

Resolved Impulse rigidbody forces different in build

Discussion in 'Multiplayer' started by G4llifreyan, Oct 7, 2021.

  1. G4llifreyan

    G4llifreyan

    Joined:
    Jun 22, 2020
    Posts:
    12
    Hello everyone,

    I'm making a fps multiplayer game and I'm working on the jumping. I came across that problem: forces added in build mode are way weaker than in edit mode.

    • Jumping in editor:

    • Jumping in build:


    I made my searches on the subject and all i found was about Time.deltaTime and not about Impulse forces. Here is the codes involved in jumping :

    • Movement script (OnCollision functions aren't important to read, they detect ground):
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Mirror;
    6.  
    7. [RequireComponent(typeof(PlayerControllerCh4riot))]
    8. public class PlayerMove : MonoBehaviour {
    9.  
    10.     [SerializeField]
    11.     private Vector3 velocity;
    12.     [SerializeField]
    13.     private Vector3 rotation;
    14.     [SerializeField]
    15.     private float cameraRotationX;
    16.     [SerializeField]
    17.     private Rigidbody body;
    18.     [SerializeField]
    19.     private Camera playerCamera;
    20.     [SerializeField]
    21.     private float cameraRotationXMax = 85f;
    22.     private float currentCameraRotationX;
    23.     [SerializeField]
    24.     private float jumpForce = 2f;
    25.     [SerializeField]
    26.     public GroundChecker groundChecker;
    27.     public bool isGrounded;
    28.  
    29.     private void Start() {
    30.         //Cursor.lockState = CursorLockMode.Locked;
    31.     }
    32.  
    33.     public void SetVelocity(Vector3 _velocity) {
    34.         velocity = _velocity;
    35.     }
    36.  
    37.     public void SetRotation(Vector3 _rotation) {
    38.         rotation = _rotation;
    39.     }
    40.     public void SetCameraRotationX(float _cameraRotationX) {
    41.         cameraRotationX = _cameraRotationX;
    42.     }
    43.  
    44.  
    45.     private bool isJumping;
    46.     private void FixedUpdate() {
    47.         Move();
    48.     }
    49.  
    50.     private void LateUpdate() {
    51.         Rotate();
    52.     }
    53.  
    54.  
    55.     public void Jump() {
    56.         if (isGrounded) body.AddForce(Vector3.up * jumpForce , ForceMode.Impulse);
    57.     }
    58.  
    59.  
    60.     private void Move() {
    61.         if (velocity != Vector3.zero) {
    62.             body.MovePosition(body.position + velocity * Time.fixedDeltaTime);
    63.         }
    64.     }
    65.  
    66.     private void Rotate() {
    67.         body.MoveRotation(body.rotation * Quaternion.Euler(rotation));
    68.  
    69.         currentCameraRotationX = Mathf.Clamp(currentCameraRotationX + cameraRotationX, -cameraRotationXMax, cameraRotationXMax);
    70.         playerCamera.transform.localEulerAngles = new Vector3(-currentCameraRotationX, 0, 0);
    71.     }
    72.  
    73.  
    74.  
    75.     private void OnCollisionEnter(Collision collision) {
    76.         Vector3 localCollisionPoint = transform.InverseTransformPoint(collision.GetContact(0).point);
    77.         /*Debug.Log("ENTER [" + (IsGroundCollision(localCollisionPoint, collision.gameObject.layer) ? "X" : " ") + "]"
    78.             + "(" + groundChecker.colliders.Count.ToString() + ")"
    79.             + "[" + collision.gameObject.name + ":" + LayerMask.LayerToName(collision.gameObject.layer) + "]"
    80.             + ": " + localCollisionPoint.ToString("F4"));*/
    81.         if (IsGroundCollision(localCollisionPoint, collision.gameObject.layer)) {
    82.             AddGroundCollider(collision.collider);
    83.         }
    84.     }
    85.  
    86.     private void AddGroundCollider(Collider collider) {
    87.         isGrounded = true;
    88.         groundChecker.colliders.Add(collider);
    89.  
    90.     }
    91.     private void OnCollisionExit(Collision collision) {
    92.         /*Debug.Log("Exit (" + groundChecker.colliders.Count.ToString() + ")"
    93.                   + "[" + collision.gameObject.name + ":" + LayerMask.LayerToName(collision.gameObject.layer) + "]"
    94.                   + "[" + (groundChecker.colliders.Contains(collision.collider) ?"x":" ") + "]");*/
    95.         groundChecker.colliders.Remove(collision.collider);
    96.         if (groundChecker.colliders.Count == 0) isGrounded = false;
    97.     }
    98.  
    99.     private void OnCollisionStay(Collision collision) {
    100.         if (!isGrounded) {
    101.             Vector3 localCollisionPoint = transform.InverseTransformPoint(collision.GetContact(0).point);
    102.             if (IsGroundCollision(localCollisionPoint, collision.gameObject.layer)) {
    103.                 AddGroundCollider(collision.collider);
    104.             }
    105.         }
    106.     }
    107.  
    108.     private bool IsGroundCollision(Vector3 point, int layer) {
    109.         return Math.Abs(point.y) < .5f && Math.Round(point.x, 4) == 0 && Math.Round(point.z, 4) == 0
    110.                && ((groundChecker.groundCheckLayers.value & (1 << layer)) > 0);
    111.     }
    112. }
    113.  
    • Commands controller script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(PlayerMove))]
    6. public class PlayerControllerCh4riot : MonoBehaviour {
    7.  
    8.     [SerializeField]
    9.     private float speed;
    10.     [SerializeField]
    11.     private PlayerMove playerMove;
    12.     [SerializeField]
    13.     private float mouseSensitivity;
    14.  
    15.     private void Update() {
    16.         // Calculates player's velocity
    17.         {
    18.             Vector3 moveHorizontal = transform.right * Input.GetAxisRaw("Horizontal");
    19.             Vector3 moveVertical = transform.forward * Input.GetAxisRaw("Vertical");
    20.             Vector3 velocity = (moveHorizontal + moveVertical).normalized * speed;
    21.             playerMove.SetVelocity(velocity);
    22.         }
    23.         // Calculates player's rotation
    24.         {
    25.             Vector3 bodyRotation = new Vector3(0, Input.GetAxisRaw("Mouse X"), 0) * mouseSensitivity;
    26.             playerMove.SetRotation(bodyRotation);
    27.             Vector3 cameraRotation = new Vector3(Input.GetAxisRaw("Mouse Y"), 0, 0) * mouseSensitivity;
    28.             playerMove.SetCameraRotationX(cameraRotation.x);
    29.         }
    30.         // Handles jump
    31.         if (Input.GetButton("Jump")) {
    32.             playerMove.Jump();
    33.         }
    34.     }
    35. }
    36.  
    If anyone has a solution, I'll take it!
     
  2. ep1s0de

    ep1s0de

    Joined:
    Dec 24, 2015
    Posts:
    168
    Change the method from Update to FixedUpdate

    The player's movements must be in a fixed update because the frequency of the call is always constant, unlike Update, which is updated as the frame is drawn
     
  3. G4llifreyan

    G4llifreyan

    Joined:
    Jun 22, 2020
    Posts:
    12
    I tried something similar before and didn't work but now it does ! Thank you so much !