Search Unity

Gravity not working

Discussion in 'Scripting' started by pandax0, Apr 22, 2021.

  1. pandax0

    pandax0

    Joined:
    Mar 7, 2021
    Posts:
    6
    Hi! I'm making my first game with Unity. It's going to be a 3D platformer but for some reason the gravity just stopped working. I can't figure out what's wrong with it, so I'm hoping somebody will be able to help. My gravity is set to -9.81. Here is the full code for the player:
    Code (CSharp):
    1.     public float speed;
    2.     public float jumpForce;
    3.     bool canJump;
    4.     CharacterController cc;
    5.     public Vector3 movement;
    6.  
    7.     void Start() // On start
    8.     {
    9.         cc = GetComponent<CharacterController>(); // Getting the CharacterController to assign it to cc
    10.     }
    11.  
    12.     void Update() // Once per frame
    13.     {
    14.         cc.Move(movement * Time.deltaTime); // Setting cc's movement to movement Vector3
    15.  
    16.         movement.y += Physics.gravity.y * Time.deltaTime; // Gravity (still bugged, fix requires loss of double jump)
    17.  
    18.         movement = ((transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"))) * speed; // Movement and controls
    19.  
    20.         // Double jumps (start)
    21.         if(cc.isGrounded) // Checking if cc is on a surface
    22.         {
    23.             if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button
    24.             {
    25.                 movement.y = jumpForce; // Jump
    26.  
    27.                 canJump = true; // Setting canJump to true for double jump
    28.             }
    29.  
    30.             canJump = true; // Setting canJump to true while on floor
    31.         }
    32.         else // Checking if cc is NOT on a surface
    33.         {
    34.             if(canJump) // Checking if canJump is true
    35.             {
    36.                 if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button
    37.                 {
    38.                     movement.y = jumpForce; // Jump
    39.  
    40.                     canJump = false; // Restricting a triple jump
    41.                 }
    42.             }
    43.         }
    44.         // Double jumps (end)
    45.     }
    Any help would be appreciated!

    PS: It needs to work with my game's double jump mechanic.
     
    Last edited: Apr 23, 2021
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    At Line 18, you are assigning a Vector3 value to movement. This overwrites the value you assigned to movement.y at Line 16. Not sure it will fix your problem, but try swapping Lines 16 and 18 with each other.
     
    PraetorBlue and Kurt-Dekker like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Just swapping the lines won't totally fix it. You'll want to fully preserve the y component of the movement variable.
     
    Last edited: Apr 22, 2021
  4. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    I think you're right. At Lines 25 and 38, he's potentially got the same problem.

    Not sure, of course, but I think what we've got here is a common error for new Unity programmers, which is thinking that changes to a variable linked to action take effect immediately. The only time it appears movement affects action is at Line 14 (which maybe ought to be moved to the end of Update, and/or moved to FixedUpdate). When computing the next movement, one needs to accumulate all the relevant drivers (gravity, player input, current velocity) by summing them. Applying them individually just means the last one you assign is the only one you'll see taking any effect.
     
  5. pandax0

    pandax0

    Joined:
    Mar 7, 2021
    Posts:
    6
    Sorry for the late response, swapping the lines didn't fix it.
     
  6. pandax0

    pandax0

    Joined:
    Mar 7, 2021
    Posts:
    6
    I tried both moving line 14 to the end of
    void Update()
    and putting it in
    void FixedUpdate()
    , but neither worked.
     
  7. pandax0

    pandax0

    Joined:
    Mar 7, 2021
    Posts:
    6
    Also, if it helps, here is a script for the camera:
    Code (CSharp):
    1.     public float sensitivity;
    2.     float horizontal;
    3.     float vertical;
    4.     float desiredXValue;
    5.     float desiredYValue;
    6.     public bool customOffset;
    7.     public Transform target;
    8.     public Transform pivot;
    9.     public Vector3 offset;
    10.     Quaternion rotation;
    11.  
    12.     void Start() // On start
    13.     {
    14.         if (!customOffset) // Checking if customOffset is NOT true
    15.         {
    16.             offset = target.position - transform.position; // Automatically setting a camera offset
    17.         }
    18.  
    19.         pivot.transform.position = target.transform.position; // Moving pivot to the target
    20.         pivot.transform.parent = target.transform; // Setting the pivot's parent to the target
    21.  
    22.         Cursor.lockState = CursorLockMode.Locked; // Locking the cursor
    23.     }
    24.    
    25.     void Update() // Once per frame
    26.     {
    27.         transform.LookAt(pivot); // Making the camera look at the pivot
    28.  
    29.         horizontal = Input.GetAxis("Mouse X") * sensitivity; // Setting the horizontal float to be Mouse X multiplied by the sensitivity
    30.         target.Rotate(0, horizontal, 0); // Rotating the y axis by horizontal
    31.  
    32.         vertical = Input.GetAxis("Mouse Y") * sensitivity; // Setting the vertical float to be Mouse Y multiplied by the sensitivity
    33.         pivot.Rotate(-vertical, 0, 0); // Rotating the x axis by vertical multiplied by -1 to make it more natural
    34.  
    35.         rotation = Quaternion.Euler(desiredXValue, desiredYValue, 0); // Rotating the camera angle
    36.         transform.position = target.position - rotation * offset; // Orbiting around the pivot
    37.  
    38.         desiredXValue = pivot.eulerAngles.x;
    39.         desiredYValue = target.eulerAngles.y;
    40.     }
     
  8. pandax0

    pandax0

    Joined:
    Mar 7, 2021
    Posts:
    6
    I just noticed something: The gravity is working, just really, really slowly. Like I have to sit there for 1 minute just to go down by 1.5. I still don't know why it's happening though... also, jumping barely does anything, but it's there. Changing the gravity all the way to -750 increases the speed of falling to normal... but why so much? Shouldn't -9.81 be enough?
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    This means your code is still overwriting the y velocity instead of letting it build up.

    Please read the earlier posts in this thread.
     
    Stevens-R-Miller likes this.
  10. pandax0

    pandax0

    Joined:
    Mar 7, 2021
    Posts:
    6
    So I managed to fix the problem. For anyone else with this issue, here are the changes that fixed it:
    Code (CSharp):
    1.     public float speed;
    2.     public float jumpForce;
    3.     float gravity;
    4.     bool canJump;
    5.     CharacterController cc;
    6.     public Vector3 movement;
    7.  
    8.     void Start() // On start
    9.     {
    10.         cc = GetComponent<CharacterController>(); // Getting the CharacterController to assign it to cc
    11.     }
    12.  
    13.     void Update() // Once per frame
    14.     {
    15.         movement = ((transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"))) * speed; // Movement and controls
    16.  
    17.         gravity += Physics.gravity.y * Time.deltaTime; // Gravity (still bugged, fix requires loss of double jump)
    18.  
    19.         movement = new Vector3(movement.x, gravity, movement.z);
    20.  
    21.         // Double jumps (start)
    22.         if(cc.isGrounded) // Checking if cc is on a surface
    23.         {
    24.             if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button
    25.             {
    26.                 movement.y = jumpForce; // Jump
    27.  
    28.                 canJump = true; // Setting canJump to true for double jump
    29.             }
    30.  
    31.             canJump = true; // Setting canJump to true while on floor
    32.  
    33.             gravity = 0;
    34.         }
    35.         else // Checking if cc is NOT on a surface
    36.         {
    37.             if(canJump) // Checking if canJump is true
    38.             {
    39.                 if(Input.GetButtonDown("Jump")) // Checking if the player pressed the jump button
    40.                 {
    41.                     movement.y = jumpForce; // Jump
    42.  
    43.                     canJump = false; // Restricting a triple jump
    44.                 }
    45.             }
    46.  
    47.             gravity += Physics.gravity.y * Time.deltaTime;
    48.         }
    49.         // Double jumps (end)
    50.  
    51.         cc.Move(movement * Time.deltaTime); // Setting cc's movement to movement Vector3
    52.     }
    This worked but the jump is a little teleporty (is that a word?) but I can fix that later.