Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Complicated and overloaded

Discussion in 'Input System' started by Karsten, May 19, 2020.

  1. Karsten

    Karsten

    Joined:
    Apr 8, 2012
    Posts:
    187
    as the title says, a simple prototyping isnt possible anymore with this monster system

    what is the quickest way to just have a horizontal and vertical axis value between +1 and -1
    pressing WASD , how to read them and from where, i ended up writing a method like
    public void OnMove(InputAction.CallbackContext context)
    {

    }

    now how to do something useful with "context"
    the documentation is bad and runs away from its own problems by
    showing an "event" Move and then there is a code but not about move its just "fire"

    here https://blogs.unity3d.com/2019/10/14/introducing-the-new-input-system/
    read and cry....

    is there any official video tutorial or similiar that shows you how to get seriously productive with this madness ?
     
  2. Bazoo_Studios

    Bazoo_Studios

    Joined:
    Dec 18, 2011
    Posts:
    92
    Ok, so I have a similar issue as I can't even figure out how to make a character jump, and no idea how animations are to come into play and work with any of this, but I wanted to try this out seeing as it is here to stay. I am a quick learner and I know programming and have been programming for a while. That being said I am using a character controller. I have the movement working so far but no idea how to implement a jump function yet. Hope this helps somehow, I saw this post had a lot of view but no replies, so hopefully someone has more info than me.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     //variables
    9.     private NewControls controls;
    10.     [SerializeField]
    11.     private float movementSpeed = 1.0f;
    12.     [SerializeField]
    13.     private float jumpSpeed = 8f;
    14.     [SerializeField]
    15.     private float gravity = 20f;
    16.     private Vector2 move;
    17.     private Vector3 movement = Vector3.zero;
    18.     //private Transform moveY;//put back later
    19.     //private InputAction jumpAction;//trying to find a way to jump
    20.     //private GameObject cam; //might not need
    21.     private CharacterController characterController;
    22.  
    23.  
    24.     // Start is called before the first frame update
    25.     void Awake()
    26.     {
    27.         //find that  boi
    28.         characterController = GetComponent<CharacterController>();
    29.  
    30.         controls = new NewControls();
    31.  
    32.         controls.Player.Jump.started += ContextCallback => Jump();
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         if(characterController.isGrounded)
    39.         {
    40.             //get the movement from the input manager
    41.             //input here is coming from the keyboard values WASD, Arrow Keys, or PS4 Left Stick
    42.             move = controls.Player.Movement.ReadValue<Vector2>();
    43.  
    44.             //NOT SURE: Might move the axis based on input direction???
    45.             movement = (move.y * transform.forward) + (move.x * transform.right);
    46.         }
    47.  
    48.         //apply gravity
    49.         movement.y -= gravity * Time.deltaTime;
    50.  
    51.         //Apply the inputs into the character controller
    52.         characterController.Move(movement * movementSpeed * Time.deltaTime);
    53.  
    54.     }
    55.  
    56.     private void Jump()
    57.     {
    58.         Debug.Log("Jump was called");
    59.         if (characterController.isGrounded)
    60.         {
    61.             Debug.Log("2nd part of Jump was called");
    62.             //send 'em yeetin
    63.             movement.y = jumpSpeed;
    64.            
    65.         }
    66.     }
    67.  
    68.     private void OnEnable()
    69.     {
    70.         controls.Enable();
    71.     }
    72.  
    73.     private void OnDisable()
    74.     {
    75.         controls.Disable();
    76.     }
    77. }
    78.  
     
  3. Bazoo_Studios

    Bazoo_Studios

    Joined:
    Dec 18, 2011
    Posts:
    92
    So I figured out my jump issue. I just made it so the input action turns a bool true and then it allows the rest to happen under update. inefficient and redundant and the most difficult way to achieve a simple task? Probably. But it works. LOL. Here is my revised code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     //variables
    9.     private NewControls controls;
    10.     [SerializeField]
    11.     private float movementSpeed = 1.0f;
    12.     [SerializeField]
    13.     private float jumpSpeed = 8f;
    14.     [SerializeField]
    15.     private float gravity = 20f;
    16.     private Vector2 move;
    17.     private Vector3 movement = Vector3.zero;
    18.     private bool canYeet;
    19.     //private Transform moveY;//put back later
    20.     //private InputAction jumpAction;//trying to find a way to jump
    21.     //private GameObject cam; //might not need
    22.     private CharacterController characterController;
    23.  
    24.  
    25.     // Start is called before the first frame update
    26.     void Awake()
    27.     {
    28.         canYeet = false;
    29.         //find that  boi
    30.         characterController = GetComponent<CharacterController>();
    31.  
    32.         controls = new NewControls();
    33.  
    34.         controls.Player.Jump.started += ContextCallback => Jump();
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.         if(characterController.isGrounded)
    41.         {
    42.             //get the movement from the input manager
    43.             //input here is coming from the keyboard values WASD, Arrow Keys, or PS4 Left Stick
    44.             move = controls.Player.Movement.ReadValue<Vector2>();
    45.  
    46.             //NOT SURE: Might move the axis based on input direction???
    47.             movement = (move.y * transform.forward) + (move.x * transform.right);
    48.  
    49.             //if statement adding the movement.y
    50.             if(canYeet == true)
    51.             {
    52.                 movement.y = jumpSpeed;
    53.                 canYeet = false;
    54.             }
    55.         }
    56.  
    57.         //apply gravity
    58.         movement.y -= gravity * Time.deltaTime;
    59.  
    60.         //Apply the inputs into the character controller
    61.         characterController.Move(movement * movementSpeed * Time.deltaTime);
    62.  
    63.     }
    64.  
    65.     private void Jump()
    66.     {
    67.         Debug.Log("Jump was called");
    68.         if (characterController.isGrounded)
    69.         {
    70.             Debug.Log("2nd part of Jump was called");
    71.             //send 'em yeetin
    72.             //have a true/false bool
    73.             canYeet = true;
    74.            
    75.         }
    76.     }
    77.  
    78.     private void OnEnable()
    79.     {
    80.         controls.Enable();
    81.     }
    82.  
    83.     private void OnDisable()
    84.     {
    85.         controls.Disable();
    86.     }
    87. }
    88.  
     
  4. Cerzi

    Cerzi

    Joined:
    Dec 28, 2014
    Posts:
    12
    If you just want to implement a binding as quickly as possible you can use
    public InputAction action
    to generate an action field in the monobehaviour's inspector.

    You can then use
    action.performed += OnActionPerformedCallback
    to respond to that action, or whatever other way of reading the action you need (ie ReadValue<T>)

    @Bazoo_Studios, for that logic you shouldn't be using a callback but just calling the "triggered" property of the action which returns true if the action was triggered this frame. eg:

    Code (CSharp):
    1. //if statement adding the movement.y
    2.             if(Controls.Player.Jump.triggered == true)
    3.             {
    4.                 movement.y = jumpSpeed;
    5.             }
     
    SqualLyuk likes this.
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    Or just don't bother with bindings and query controls directly:

    Code (CSharp):
    1. if (Keyboard.current.wKey.isPressed) {
    2.     // ...
    3. }
    4.  
    5. if (Keyboard.current[Key.S].wasPressedThisFrame) {
    6.     // ...
    7. }