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. Dismiss Notice

Is this alright as a movement script?

Discussion in 'Scripting' started by Gramms66, Sep 22, 2020.

  1. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    Hello!
    I was always unsure about how to use fixedupdate correctly, now I'm trying to make a 2D platformer and I want to movement system to be as polished as possible, so I made a simple movement script but I'm not sure if I use fixedupdate correctly here. Do I? If not what should I do?

    Code (CSharp):
    1.     [SerializeField] private float speed = 2f;
    2.     [SerializeField] private float maxSpeed = 6f;
    3.     [SerializeField] private float friction = .15f;
    4.  
    5.     private Vector2 currentVelocity = Vector2.zero;
    6.  
    7.     private Rigidbody2D myRb;
    8.  
    9.     private float xInput, yInput;
    10.  
    11.     void Start()
    12.     {
    13.         myRb = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         GetInput();
    19.     }
    20.  
    21.     private void GetInput()
    22.     {
    23.         xInput = Input.GetAxis("Horizontal");
    24.  
    25.         //Calculating velocity
    26.         if (Mathf.Abs(currentVelocity.x) < maxSpeed)
    27.         {
    28.             Vector2 newVelocity = new Vector2(xInput, myRb.velocity.y) * speed * Time.deltaTime;
    29.             currentVelocity += newVelocity;
    30.         }
    31.  
    32.     }
    33.  
    34.     private void FixedUpdate() {
    35.         UpdateVelocity();
    36.     }
    37.  
    38.     private void UpdateVelocity()
    39.     {      
    40.         //Friction
    41.         if (Input.GetAxisRaw("Horizontal") == 0 || (xInput > 0 && myRb.velocity.x < 0) || (xInput < 0 && myRb.velocity.x > 0))
    42.         {
    43.             currentVelocity.x = Mathf.Lerp(currentVelocity.x, 0f, friction);
    44.         }
    45.  
    46.         currentVelocity.x = Mathf.Clamp(currentVelocity.x, -maxSpeed, maxSpeed);
    47.     }
    48.  
    49.        myRb.velocity = currentVelocity;
     
  2. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    I noticed a little mistake but unity forums wont let me edit the post as it is "Spamming" as it believes so I just post the correct code in a reply

    Code (CSharp):
    1.     [SerializeField] private float speed = 2f;
    2.     [SerializeField] private float maxSpeed = 6f;
    3.     [SerializeField] private float friction = .15f;
    4.  
    5.     private Vector2 currentVelocity = Vector2.zero;
    6.  
    7.     private Rigidbody2D myRb;
    8.  
    9.     private float xInput, yInput;
    10.  
    11.     void Start()
    12.     {
    13.         myRb = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         GetInput();
    19.     }
    20.  
    21.     private void GetInput()
    22.     {
    23.         xInput = Input.GetAxis("Horizontal");
    24.  
    25.         //Calculating velocity
    26.         if (Mathf.Abs(currentVelocity.x) < maxSpeed)
    27.         {
    28.             Vector2 newVelocity = new Vector2(xInput, myRb.velocity.y) * speed * Time.deltaTime;
    29.             currentVelocity += newVelocity;
    30.         }
    31.  
    32.     }
    33.  
    34.     private void FixedUpdate() {
    35.         UpdateVelocity();
    36.     }
    37.  
    38.     private void UpdateVelocity()
    39.     {    
    40.         //Friction
    41.        if (Input.GetAxisRaw("Horizontal") == 0 || (xInput > 0 && myRb.velocity.x < 0) || (xInput < 0 && myRb.velocity.x > 0))
    42.         {
    43.             currentVelocity.x = Mathf.Lerp(currentVelocity.x, 0f, friction);
    44.         }
    45.  
    46.         currentVelocity.x = Mathf.Clamp(currentVelocity.x, -maxSpeed, maxSpeed);
    47.  
    48.         myRb.velocity = currentVelocity;
    49.     }
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    You should be using xInput instead of Input.GetAxisRaw on line 41, but otherwise it's pretty good.
     
    Gramms66 likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Does it work? Then ship it. :)

    If you like some feedback, gather ALL the input in one function. You're actually gathering input from the same axis in two places.

    ALSO: GetAxisRaw() might never actually return zero. It might drop to 0.0001 and never fire those lines of code. Instead, check for something like
    Mathf.Abs( xInput) < 0.1f
    as a condition.
     
    Gramms66 likes this.
  5. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    @Kurt-Dekker
    @StarManta

    There seems to be a problem after all.
    Lines 28. 29. messes up the player's y movement. I want this not to affect the y velocity at all, but it does and weird behaviour happens. On line 28. I've tried changing myRb.velocity.y to 0f but it stops the player in the air, which is quite odd because logically for me if you add 0 to something it shouldn't affect it at all.

    How could I change my code so the Y velocity won't be affected?
     
  6. Gramms66

    Gramms66

    Joined:
    Aug 15, 2017
    Posts:
    31
    I think i found a solution

    Code (CSharp):
    1.         //Calculating velocity
    2.         if (Mathf.Abs(currentVelocity.x) < maxSpeed)
    3.         {
    4.             Vector2 newVelocity = new Vector2(xInput, 0f) * speed * Time.deltaTime;
    5.             currentVelocity += newVelocity;
    6.             currentVelocity.y = myRb.velocity.y;
    7.         }