Search Unity

Question Beginner at new input system, character doesn't move?

Discussion in 'Input System' started by Deleted User, Sep 10, 2022.

  1. Deleted User

    Deleted User

    Guest

    Hello. I am working on a game with more physics-based fighting, and wanted to try the new Input system- which is really not new anymore, but...

    I have the input system for the player to have two different positive and negative bindings- one for the X Axis, and one for the Y axis. I would like to, for a test, have the player move left and right, swim down, and swim up or jump in the 2D water. At this stage, it doesn't even move. FYI, speed = 10, and the PlayerController is actually the Input system. I have this in the Update() function. Movement is a Vector2.zero which is supposed to be the one that contains the values for the PlayerController to read.

    PlayerController.ReadValue<Vector2>();
    RB.velocity = new Vector2(movement.x * speed, movement.y * speed);

    Again, this character does not budge. The rigidBody2D has a mass of 1, so it isn't about that- I just switched it from its original number, which was 2. I checked and the everything is bound to the arrow keys. What am I missing here?

    Thank you. Screenshot (581).png Screenshot (583).png
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,050
    Show the full code using code tags
     
  3. Deleted User

    Deleted User

    Guest

    The speed and jumpHeight are from using the old input system, though I can still use the speed.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerScript : MonoBehaviour
    7. {
    8.  
    9.     public float speed = 10;
    10.     public float jumpHeight = 2;
    11.  
    12.     float modifier = 1f;
    13.     float vertModifier;
    14.     Rigidbody2D RB;
    15.     bool inWater;
    16.     bool onGround;
    17.  
    18.     bool isColliding;
    19.  
    20.     public InputAction PlayerController;
    21.  
    22.     Vector2 movement = Vector2.zero;
    23.  
    24.     private void OnEnable()
    25.     {
    26.         PlayerController.Enable();
    27.     }
    28.  
    29.     private void OnDisable()
    30.     {
    31.         PlayerController.Disable();
    32.     }
    33.  
    34.     // Start is called before the first frame update
    35.     void Start()
    36.     {
    37.         RB = gameObject.GetComponent<Rigidbody2D>();
    38.     }
    39.  
    40.     private void Update()
    41.     {
    42.         onGround = GroundCheck.onGround;
    43.         inWater = GroundCheck.inWater;
    44.         isColliding = HeadCheck.isColliding;
    45.  
    46.         PlayerController.ReadValue<Vector2>();
    47.         RB.velocity = new Vector2(movement.x * speed, movement.y * speed * 2);
    48.     }
    49.  
    50.     // Update is called once per frame
    51.     void FixedUpdate()
    52.     {
    53.     }
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,050
    You read the value but don't use it or cache it, so obviously it won't work.
    Also put the values in debug.logs to see what they are so you can understand your code better
     
  5. Deleted User

    Deleted User

    Guest

    I'm lost. I never heard of cache in Unity before. Plus I've only ever coded objects to move before- not just in Unity, so this is very new.

    I thought you meant something like this, but it still won't budge.

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         onGround = GroundCheck.onGround;
    4.         inWater = GroundCheck.inWater;
    5.         isColliding = HeadCheck.isColliding;
    6.  
    7.         movement = PlayerController.ReadValue<Vector2>();
    8.         Debug.Log(movement.x);
    9.         RB.velocity = new Vector2(movement.x * speed, movement.y * speed * 2);
    10.     }
    11.  
    I snagged it from another code of the forum.

    1. using UnityEngine;
    2. using UnityEngine.InputSystem;

    3. public class PlayerMovement : MonoBehaviour
    4. {
    5. private Vector2 m_PlayerMovement;
    6. private InputAction m_MoveAction;
    7. private InputAction m_AttackAction;

    8. void Start()
    9. {
    10. m_MoveAction = new InputAction("Move");
    11. m_MoveAction.AddBinding("<Gamepad>/leftStick");
    12. m_MoveAction.Enable();

    13. m_AttackAction = new InputAction("Attack", binding: "<Gamepad>/buttonSouth");
    14. m_AttackAction.Enable();
    15. }

    16. void Update()
    17. {
    18. m_PlayerMovement = m_MoveAction.ReadValue<Vector2>();
    19. if(m_PlayerMovement != Vector2.zero)
    20. Debug.Log("Vector = " + m_PlayerMovement);
    21. var attacking = m_AttackAction.ReadValue<float>();
    22. if(Mathf.Approximately(attacking, 1f))
    23. Debug.Log("Attacking");
    24. }
    25. }
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,050
    With cache I ment having a local variable which you store the vector into.

    What does the debug log give?
    Did you google '2d character controller new input system' already? Found many guides there
     
  7. Deleted User

    Deleted User

    Guest

    I don't have a character controller, it's a rigidbody2d with an input system named PlayerController. As stated before, this has a LOT of physics. A character controller, I fear, won't be enough.

    I get a bunch of zeros on the Debug.log.
     
    Last edited by a moderator: Sep 10, 2022
  8. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,050
    That meant the action isn't bound.

    And a RigidBody2D that controlls a character can also be concidered a character controller (;
    Just look up some guides since these are the basics
     
  9. Deleted User

    Deleted User

    Guest

    Sorry, I thought you meant the actual component character controller.

    And believe me when I say I already looked for guides- on the Unity website, on Youtube...
     
  10. Deleted User

    Deleted User

    Guest

    On the flip side, are they planning on removing the old system any time soon? If not, I could just use that.
     
  11. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,050
    Not that I know, feel free to use it
     
  12. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
  13. Deleted User

    Deleted User

    Guest

    I read the docs, and I don't have a joystick or gamepad. I want to move with arrow keys, WASD, and space for a 2D platformer. I don't know how to tell the device to use the actions for the basic movements.
     
  14. BACALL

    BACALL

    Joined:
    Feb 21, 2017
    Posts:
    87
    Which Unity version do you use? If I update my project to the latest Unity and Input version I no longer get input for Vector2s, but it do work in previous versions. Could be related to this: https://forum.unity.com/threads/what-are-the-causes-of-these-errors.1302582/#post-8416839
     
  15. Deleted User

    Deleted User

    Guest

    I have Unity 2021.3.6f1...

    Good to know.
     
  16. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Movement isn't thought of as a button, but a value. Specifically a vector 2 value. You can then use a composite binding for up/down/left/right to define WASD. You can also add gamepad and joystick to that action. This can all be done in the input action asset.

    I suggest you follow a more recent tutorial on youtube. The Input system isn't that hard to use, you just have to learn how it works a bit.