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

Resolved WasPressedThisFrame not working with new input system

Discussion in 'Scripting' started by wileyjerkins, Jul 16, 2021.

  1. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Hello again! I have returned after the smash success of the Star Runner game on Steam (it was a flop, actually).

    I am trying out the new input system, and I must say it is complete horseshit. I am determined to master it, however, because I like feeling smart.

    My current problem is I can't get the new "WasPressedThisFrame" thing to work.

    Here is my code, called via an action event (I think that is the correct terminology), which works fine except for the WasPressedThisFrame part ... I am trying to use the shift keys to turbo-charge the camera zoom in and out.

    Thanks for any assistance in this matter,
    GM

    Code (CSharp):
    1. private void OnCam(InputValue mVal)
    2.     {
    3.         float mFloat = mVal.Get<float>();
    4.         //Debug.Log(mFloat);
    5.  
    6.         float minY = 40;
    7.         float maxY = 200;
    8.         float camSpeed = 2f;
    9.  
    10.         if (Keyboard.current.leftShiftKey.wasPressedThisFrame) { camSpeed = 5f; }
    11.         if (Keyboard.current.rightShiftKey.wasPressedThisFrame) { camSpeed = 5f; }
    12.  
    13.         float newCamY = mainCamera.transform.position.y;
    14.  
    15.         if (mFloat > 0) { newCamY -= camSpeed; }
    16.         if (mFloat < 0) { newCamY += camSpeed; }
    17.  
    18.         if (newCamY > maxY) { newCamY = maxY; }
    19.         if (newCamY < minY) { newCamY = minY; }
    20.  
    21.         //Debug.Log(newCamY);
    22.  
    23.         Vector3 newCamPos = new Vector3(mainCamera.transform.position.x, newCamY, mainCamera.transform.position.z);
    24.         mainCamera.transform.position = newCamPos;
    25.  
    26.         mainCamera.transform.LookAt(gameObject.transform); //look at player
    27.     }
     
    Yoreki likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Don't worry, there's a lot of that going around. While you typed that post, 27 new games were shipped worldwide. Or maybe 270 new games. Not sure.

    There are about 27 more dependencies and fiddling annoyances, but In Theory(tm) it's better. In practice there's about 27,000 posts per day of people saying "it doesn't work," so again you're in good company.

    All I can say is the same for any new API: go back and methodically work through an example.

    https://stackoverflow.com/questions...alent-of-getkey-in-the-new-unity-input-system
     
    wileyjerkins likes this.
  3. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Thanks, I have done that several times. Oh well. I do like the IDEA of the new system, but who in their right mind would use it? If you were writing one codebase for every device under the sun ... maybe. Otherwise you are creating a massive headache for yourself (if you already know the old system).
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    Don't fix what ain't broke.

    If you're already familiar with the old system, and it behaves the way you want it to, continue using that.

    ...

    Over the years I've created my own stuff. Such as my own advanced input system, as well as my own sort-of UnityEvent (SPEvent) before UnityEvent existed. Now that these new systems exist I've actually had people in comments ask why I'd continue using my SPEvent since Unity now has one. And it's for this very reason... why change a work-flow that I already have that works?

    If you want to tinker with the new system just to get familiar with it. Have at it. But if you take the approach of "I'm doing this to learn it for funsies" rather than "I need to make this work in my game". It may be a smoother experience for you because if you get frustrated you can just put it down, walk away, and work on your game instead.
     
  5. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Thanks, this is definitely just a learning experience type thing, probably not aiming to make a game. That does not pay well enough! I like tinkering with the new Unity "features" to see what they can do, but this one is a real headscratcher.

    Thanks for your reply here (and in many other questions where you have proven helpful).
     
    lordofduct likes this.
  6. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Well, I had some spare time at lunch and re-invented the wheel. The following code works ... holding left shift turbo charges the camera zoom. Releasing the left shift key returns it to normal speed. I am sure there is a way to create a composite key for both shift keys, yet to look into that.

    This code contains the relevant stuff to make it work. Hope someone finds it useful. This is probably not how it is supposed to work, but digging through the docs ... this is what I came up with.

    The key (for me) was to create a new InputAction in Start() to detect the shift key being held and then reading that InputAction as a float to see if it was being held still.

    Here are those two nuggets of code for the TLDR crowd
    Code (CSharp):
    1. //detect shift key being held
    2.         detectLeftShiftKey = new InputAction(
    3.             type: InputActionType.Button,
    4.             binding: "<Keyboard>/leftShift",
    5.             interactions: "hold(duration=0.2)");
    6.  
    7.         detectLeftShiftKey.Enable();
    8.  
    9. ...
    10.  
    11. //the trick here is to read the key value as a float (continous value) and not trigger (non-continuous value)
    12.         if (detectLeftShiftKey.ReadValue<float>() != 0){
    13.             camSpeed = 5f;
    14.         }

    COMPLETE SCRIPT
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class player : MonoBehaviour
    7. {
    8.     //camera movement
    9.     public Camera mainCamera;
    10.     private float camMoveX;
    11.     private float camMoveY;
    12.     private InputAction detectLeftShiftKey;
    13.  
    14.     //player movement
    15.     public float speed = 0;
    16.     private Rigidbody rb;
    17.     private float movementX;
    18.     private float movementY;
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody>();
    23.  
    24.         //detect shift key being held
    25.         detectLeftShiftKey = new InputAction(
    26.             type: InputActionType.Button,
    27.             binding: "<Keyboard>/leftShift",
    28.             interactions: "hold(duration=0.2)");
    29.  
    30.         detectLeftShiftKey.Enable();
    31.     }
    32.  
    33.     private void FixedUpdate()
    34.     {
    35.         //apply player movement
    36.         Vector3 movement = new Vector3(movementX, 0.0f, movementY);
    37.         rb.AddForce(movement * speed);
    38.     }
    39.  
    40.     private void OnMove(InputValue movementValue)
    41.     {
    42.         //determines player movement
    43.         Vector2 movementVector = movementValue.Get<Vector2>();
    44.         Debug.Log(movementVector);
    45.         movementX = movementVector.x;
    46.         movementY = movementVector.y;
    47.     }
    48.  
    49.     private void OnCam(InputValue mVal)
    50.     {
    51.         float mFloat = mVal.Get<float>();
    52.  
    53.         float minY = 40;
    54.         float maxY = 200;
    55.         float camSpeed = 2f;
    56.  
    57.         //the trick here is to read the key value as a float (continous value) and not trigger (non-continuous value)
    58.         if (detectLeftShiftKey.ReadValue<float>() != 0){
    59.             camSpeed = 5f;
    60.         }
    61.  
    62.         float newCamY = mainCamera.transform.position.y;
    63.  
    64.         if (mFloat > 0) { newCamY -= camSpeed; }
    65.         if (mFloat < 0) { newCamY += camSpeed; }
    66.  
    67.         if (newCamY > maxY) { newCamY = maxY; }
    68.         if (newCamY < minY) { newCamY = minY; }
    69.  
    70.         Vector3 newCamPos = new Vector3(mainCamera.transform.position.x, newCamY, mainCamera.transform.position.z);
    71.         mainCamera.transform.position = newCamPos;
    72.  
    73.         mainCamera.transform.LookAt(gameObject.transform); //look at player
    74.     }
    75. }
     
    balthazardanger likes this.