Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Gravity not being properly applied on movement script

Discussion in 'Scripting' started by ripefruits22, Jan 11, 2022.

  1. ripefruits22

    ripefruits22

    Joined:
    Nov 26, 2021
    Posts:
    17
    I'm relatively new to programming and I haven't been able to successfully get my movement script to apply gravity, except for when gravity is set to 100,000+, and even then it acts as a velocity, not an acceleration:
    Code (CSharp):
    1. public class MovementScript : MonoBehaviour
    2. {
    3.     private CharacterController characterController;
    4.     public Transform Camera;
    5.  
    6.     public int speed;
    7.     public int gravity;
    8.     public int numMaxJumps;
    9.     public int jumpSpeed;
    10.     private int numCurrentJumps;
    11.     private float curSpeedX;
    12.     private float curSpeedZ;
    13.  
    14.  
    15.     private Vector3 moveVector;
    16.     private Vector3 velocityVector;
    17.  
    18.     public KeyCode Jump;
    19.     public KeyCode Forwards;
    20.     public KeyCode Backwards;
    21.     public KeyCode Left;
    22.     public KeyCode Right;
    23.     public KeyCode Mouselock;
    24.  
    25.     void Start()
    26.     {
    27.         characterController = GetComponent<CharacterController>();
    28.         Cursor.lockState = CursorLockMode.None;
    29.         numMaxJumps = 1;
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         velocityVector = Movement();
    35.         velocityVector.y -= gravity * Time.deltaTime;
    36.         characterController.Move(velocityVector * Time.deltaTime);
    37.         if(characterController.isGrounded && numCurrentJumps != numMaxJumps)
    38.         {
    39.             numCurrentJumps = numMaxJumps;
    40.         }
    41.     }
    42.  
    43.     public Vector3 Movement()
    44.     {
    45.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    46.         Vector3 right = transform.TransformDirection(Vector3.right);
    47.         //Sets the movement values
    48.         curSpeedX = (Input.GetKey(Forwards)) ? speed : 0;
    49.         curSpeedX = (Input.GetKey(Backwards)) ? -speed : 0;
    50.         //sets to zero if both keys are pressed
    51.         if (Input.GetKey(Forwards) && Input.GetKey(Backwards))
    52.         {
    53.             curSpeedX = 0;
    54.         }
    55.  
    56.         curSpeedZ = (Input.GetKey(Right)) ? speed : 0;
    57.         curSpeedZ = (Input.GetKey(Left)) ? -speed : 0;
    58.         if (Input.GetKey(Right) && Input.GetKey(Left))
    59.         {
    60.             curSpeedZ = 0;
    61.         }
    62.  
    63.         moveVector = (forward * curSpeedX) + (right * curSpeedZ);
    64.  
    65.         if (Input.GetKey(Jump) && (characterController.isGrounded || numCurrentJumps != 0))
    66.         {
    67.             moveVector.y = jumpSpeed;
    68.             numCurrentJumps--;
    69.         }
    70.  
    71.         return moveVector;
    72.     }
    73. }
     
  2. ripefruits22

    ripefruits22

    Joined:
    Nov 26, 2021
    Posts:
    17
    I applied the gravity in a separate script and that seemed to fix it.
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You are handling your movement weirdly. Does that even work? Let's assume i press forward.
    Your curSpeedX will be set to speed. But in the line afterwards you overwrite that, and since i do not press backwards, it will be set to 0 instead, despite me pressing forward.

    We have input axis to make all of this a whole lot easier.
    https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

    You usually just need to get that value for both axis, optionally normalize this movement vector, and then simply apply it multiplied with your speed and deltaTime.

    As for your main problem: you multiply your gravity with deltaTime twice. So yeah.. the result will be tiny. Where? Well you set velocityVector.y to gravity * Time.deltaTime, and afterwards apply movement while multiplying velocityVector * Time.deltaTime again. This has nothing to do with which script you apply your values in. Just with the size of the actual value you apply ;)

    When you are unsure about why something happens, use Debug.Log to print some values to gain additional information. Here you would have easily seen that, for example, velcoityVector contains a y-component orders of magnitude smaller than the x- or z values. Which would have been a nice hint as for what is happening.

    All in all i would highly recommend you to watch some basic tutorials on character movement. There are many good ones you can check out, which will help you to get into the topic with less confusion.

    Just one example of the many options you will find (script starts at 6:44)
     
    GroZZleR likes this.