Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved My character is constantly moving left

Discussion in 'Physics' started by PGrunwald1410, May 16, 2024.

  1. PGrunwald1410

    PGrunwald1410

    Joined:
    May 16, 2024
    Posts:
    4
    Hi! Im new to unity and i wanted to just make my character going right and left. Unfortunately, even though i've written exactly the same as in the tutorial, my character keeps sliding left with great speed.
    Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     Rigidbody2D _rb;
    8.  
    9.     float _walkSpeed;
    10.     float _inputHorizontal;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         _rb = gameObject.GetComponent<Rigidbody2D>();
    16.  
    17.         _walkSpeed = 5.5f;
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         _inputHorizontal = Input.GetAxisRaw("Horizontal");
    25.  
    26.         if (_inputHorizontal != 0)
    27.         {
    28.             _rb.AddForce(new Vector2(_inputHorizontal * _walkSpeed, 0f));
    29.         }
    30.     }
    31. }
    32.  
    and here is the character named Player

    upload_2024-5-16_20-4-50.png
     
  2. acedric1

    acedric1

    Joined:
    Feb 23, 2024
    Posts:
    3
    Also new here. I've found scattering Debug.Log() around helps me identify the problems easier. On my viewing, I think I've spotted the problem.
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         _inputHorizontal = Input.GetAxisRaw("Horizontal");
    4.         if (_inputHorizontal != 0)     //< _inputHorizontal could be null or any other non-zero and still affect the movement
    5.         {
    6.             _rb.AddForce(new Vector2(_inputHorizontal * _walkSpeed, 0f));
    7.         }
    8.     }
    consider Debug.Log(_inputHorizontal) to check if it is actually 0 when you don't use it.
     
  3. PGrunwald1410

    PGrunwald1410

    Joined:
    May 16, 2024
    Posts:
    4
    upload_2024-5-16_21-11-36.png

    thats the output i get when i don't press anything. For some reason tho, when i press right arrow, the output is 1. do you have any idea of how to remove the previous input?

    EDIT:
    I have retracted exactly the same value that is being inputed and it works perfectly fine, but the input is still there
     
  4. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    126
    You shouldn't need to remove the previous input, it's weird to me that it has the same value every frame. It should be exactly 0 when there's no key pressed, exactly -1 or exactly +1 if left or right are pressed.
    Do you have only keyboard, no gamepads etc attached? I'm not sure if GetAxisRaw applies a deadzone to stick inputs (probably not?); although -0.78 is huge, way outside of normal deadzone range. I wonder if something weird has ended up in your InputManager settings?

    I think you should do the _rb.AddForce from FixedUpdate btw, otherwise the force you're adding will vary with framerate.
     
  5. PGrunwald1410

    PGrunwald1410

    Joined:
    May 16, 2024
    Posts:
    4
    I have had my joystick plugged, after disconnecting it, the output when i dont press anything is -1
     
  6. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    126
    That's also pretty weird, the output when you don't press anything should be 0.
    Have you modified your Input manager settings somehow? Like, the defaults from a new project are good as a beginning. E.g., an empty project, if you just run a script that does Debug.Log(Input.GetAxisRaw("Horizontal")) you should see -1, 0, 1 for left, nothing, right.
     
    PGrunwald1410 likes this.
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,647
    Use the New Input System to rule out the old Input setup having weird setup or issues.

    It sounds like you have some kind of hardware drift or deeply incorrect calibration outside of Unity that is causing bad values, but the Input could be configured wrong too. Either way, you should move to New Input System, it's generally better across the board.
     
    PGrunwald1410 likes this.
  8. PGrunwald1410

    PGrunwald1410

    Joined:
    May 16, 2024
    Posts:
    4
    thanks, when i just redone my project it worked, idk, some wierd bug, but thanks for all your help