Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Rigidbody Jumping not working properly

Discussion in 'Physics' started by BreakfastPancakes, Nov 23, 2020.

  1. BreakfastPancakes

    BreakfastPancakes

    Joined:
    Jul 8, 2019
    Posts:
    3
    I'm writing a Rigidbody First Person Controller, and Sometimes it jumps higher then others. I have no idea how to solve this. Im also kind of new to scripting and Im kind of bad, so any help would be appricaited.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PhysicsBasedFPS : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.     public float WalkSpeed;
    9.     float xInput = Input.GetAxis("Horizontal");
    10.     float zInput = Input.GetAxis("Vertical");
    11.  
    12.     //Movement
    13.  
    14.     float CamX, CamY, MXR;
    15.     public Transform Camera;
    16.     public float Sensitivity;
    17.     public float LookMin, LookMax;
    18.     public float MaxSpeed;
    19.  
    20.     //Camera Stuff
    21.  
    22.     public Transform GroundCheck;
    23.     private bool isGrounded;
    24.     public float GroundCheckRadius;
    25.     public float JumpHieght;
    26.     public LayerMask WhatisGround;
    27.     public float CrouchHight;
    28.     public float RegularHight;
    29.     private bool CanUnCrouch;
    30.     private bool isCrouching;
    31.     public Transform CeelingCheck;
    32.  
    33.     //Jumping and Crouching
    34.  
    35.     private float MoveSpeed;
    36.     public float SprintSpeed;
    37.  
    38.     //Walking and Sprinting
    39.  
    40.     private float defualtdrag = 0.0f;
    41.     public float CounterDrag;
    42.     private Vector2 movement;
    43.     private Vector2 prevMovement;
    44.     //Counter Movement
    45.     private void Start()
    46.     {
    47.         rb = GetComponent<Rigidbody>();
    48.         Cursor.visible = false;
    49.         Cursor.lockState = CursorLockMode.Locked;
    50.     }
    51.     private void FixedUpdate()
    52.     {
    53.         Movement();
    54.         JumpAndCrouch();
    55.         //Check for the ground
    56.         isGrounded = Physics.CheckSphere(GroundCheck.transform.position, GroundCheckRadius, WhatisGround);
    57.         CanUnCrouch = Physics.CheckSphere(CeelingCheck.transform.position, GroundCheckRadius, WhatisGround);
    58.     }
    59.     private void Update()
    60.     {
    61.         GetInput();
    62.         CameraControls();
    63.         if (Input.GetKeyDown(KeyCode.Q))
    64.         {
    65.             Debug.Log(isGrounded);
    66.         }
    67.     }
    68.     void GetInput()
    69.     {
    70.         xInput = Input.GetAxis("Horizontal");
    71.         zInput = Input.GetAxis("Vertical");
    72.         CamX = Input.GetAxis("Mouse X") * Sensitivity;
    73.         CamY = Input.GetAxis("Mouse Y") * Sensitivity;
    74.     }
    75.     void CameraControls()
    76.     {
    77.         MXR -= CamY;
    78.         MXR = Mathf.Clamp(MXR, LookMin, LookMax);
    79.  
    80.         Camera.transform.localRotation = Quaternion.Euler(MXR, 0f, 0f);
    81.         transform.Rotate(Vector3.up * CamX);
    82.     }
    83.     void JumpAndCrouch()
    84.     {
    85.         if (isGrounded == true && Input.GetButtonDown("Jump"))
    86.         {
    87.             rb.AddForce(transform.up * JumpHieght * 50);
    88.         }
    89.         if (Input.GetKey(KeyCode.LeftControl))
    90.         {
    91.             transform.localScale = new Vector3(1, CrouchHight, 1);
    92.             isCrouching = true;
    93.         }
    94.         else
    95.         {
    96.             if(CanUnCrouch == false)
    97.             {
    98.                 transform.localScale = new Vector3(1, RegularHight, 1);
    99.                 isCrouching = false;
    100.             }
    101.         }
    102.     }
    103.     private void Movement()
    104.     {
    105.         rb.AddForce(transform.forward * zInput * MoveSpeed);
    106.         rb.AddForce(transform.right * xInput * MoveSpeed);
    107.         movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    108.         //Countermovement (HUGE thanks to alwayscodeangry on answers.unity.com, the page I found this on is here https://answers.unity.com/questions/1701997/rigidbody-controller-counter-movement.html?sort=votes)
    109.         if ((prevMovement.x != movement.x || prevMovement.y != movement.y)  || rb.velocity.magnitude > MaxSpeed)
    110.         {
    111.             rb.drag = CounterDrag;
    112.         }
    113.         else
    114.         {
    115.             rb.drag = defualtdrag;
    116.         }
    117.         prevMovement = movement;
    118.         //End Countermovement code
    119.  
    120.         if (Input.GetKey(KeyCode.LeftShift))
    121.         {
    122.             MoveSpeed = SprintSpeed;
    123.             Debug.Log("I got Here!");
    124.         } else
    125.         {
    126.             MoveSpeed = WalkSpeed;
    127.         }
    128.     }
    129.  
    130. }
    131.