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

Question Not able to move a Rigidbody2D using the Input System

Discussion in 'Physics' started by NateBob, Aug 25, 2022.

  1. NateBob

    NateBob

    Joined:
    Dec 22, 2020
    Posts:
    2
    I am trying to create a character controller for a 2D platformer (I am handling the inputs with the "new" input system.)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class LBMovement : MonoBehaviour
    5. {
    6.     public InputManager controls;
    7.     Rigidbody2D LBRigidbody;
    8.     public float jumpHeight = 5f;
    9.     public float walkSpeed = 5f;
    10.  
    11.     private void Awake()
    12.     {
    13.         LBRigidbody = GetComponent<Rigidbody2D>();
    14.         controls = new InputManager();
    15.  
    16.         controls.Player.LittleBJump.performed += Jump;
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         Vector2 direction = controls.Player.LittleBMovement.ReadValue<Vector2>();
    22.         float directionX = direction.x;
    23.    
    24.         LBRigidbody.velocity = new Vector2(directionX, 0f) * walkSpeed;
    25.     }
    26.  
    27.     private void Jump(InputAction.CallbackContext context)
    28.     {
    29.         if (context.performed)
    30.         {
    31.             Debug.Log("Jump");
    32.  
    33.             LBRigidbody.velocity = new Vector2(0f, jumpHeight);
    34.         }
    35.        
    36.     }
    37.  
    38.     private void OnEnable()
    39.     {
    40.         controls.Enable();
    41.     }
    42.  
    43.     private void OnDisable()
    44.     {
    45.         controls.Disable();
    46.     }
    47. }
    I managed to make it so that when you press the right or left arrow keys, the player moves accordingly. However, when I press the up arrow key, it prints a message saying "Jump," but it doesn't actually jump. I tried messing with the Rigidbody's gravity and the mass, as well as the jumpHeight variable, in case it was an issue of the force not being large enough, but nothing seemed to work.
    Since it prints the message, I know that it is executing the function, but its not moving the Rigidbody like it's supposed to. I already tried using AddForce instead of velocity, but it didn't do anything either. Does anyone know how I might fix this? Its probably super simple, but this is my first game. Thanks!

    (Oh, and on a side note, I noticed that when I started using velocity instead of AddForce, the Player started falling super slowly. I was able to fix this by increasing the gravity scale, but I'm worried that might have other unforeseen consequences. Is there any other way I can fix that?)
     
  2. NateBob

    NateBob

    Joined:
    Dec 22, 2020
    Posts:
    2
    Never mind I fixed it!
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Because gravity is just adding that gravity "vector" to the velocity of the body (scaled by time of course). If you're stomping over the body velocity by setting it then what's it supposed to do? ;) Essentially you're resetting velocity. Anything that changes previously made are wiped out.

    If gravity is only in the Y direction component and you're only setting the X direction component then you need to set the Y direction component to what it already is.