Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chromodyne · Jul 12, 2020 at 09:54 PM · inputjumpingeventscallbackinputs

Implementing Jump w/ New Input System

I'm attempting to implement jumping in my 2D platformer. The way I'm doing it works but it has numerous issues. I'm using the new Unity input system with unity events as my method of checking input; however, I'm not sure how to get the equivalent of GetKeyDown. With the following code the player jump input is read MANY times due to jumpInput equaling 1f numerous times per key press. (Since humans can't exactly hit the key for only one frame.)


 public void JumpInput(InputAction.CallbackContext context) {
             jumpInput = context.ReadValue<float>();
 }
 
 private void ProcessJumping() {
 
         Vector2 jumpVelocity = new Vector2(0f, jumpHeight);
 
         if (jumpInput == 1) {
             rb.velocity += jumpVelocity;
         }
 
         Vector2 vel = rb.velocity;
         vel.y += Physics2D.gravity.y;
 
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RyanRogers · Jul 13, 2020 at 12:59 AM

you can check for a bool with inputAction.PlayerControls.Jump.triggered, assuming your input action is set up to work like a button. This will cause it to fire once on the frame, and not again.

You still need to do the OnDisable and OnEnable functions mentioned earlier, but this gets you as close to the old Input.GetKeyDown as you can get.

EDIT: The code would look like this:

 using UnityEngine.InputSystem;
            
 private void Awake() {
     PlayerInputActions inputAction = new PlayerInputActions()
 }
         
 private void Update(){
     if (inputAction.PlayerControls.Jump.triggered) {
         Jump();
     }
         
 private void Jump() {
     *Do your jump here*
 }
               
 private void OnEnable() {
     inputAction.Enable();
 }
               
 private void OnDisable() {
     inputAction.Disable();
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chromodyne · Jul 13, 2020 at 10:49 PM 0
Share

What data type are you setting up your inputAction variable as? That's where I'm getting confused.

avatar image finnjwohner Chromodyne · Jul 13, 2020 at 11:16 PM 0
Share

The data type is the just whatever you called your Input Actions that you made in the assets,

alt text

$$anonymous$$g here you can see I called my one PlayerInputActions, so that's what the data type will be called, yours will be whatever you called yours.

avatar image Chromodyne finnjwohner · Jul 13, 2020 at 11:54 PM 0
Share

Thanks for the reply. Okay so the issue was that i didn't generate the C# class when i made the input actions; therefore, my script didn't know what to reference.

avatar image
1

Answer by finnjwohner · Jul 12, 2020 at 10:22 PM

I'm not too familiar with the new input system so this may not be the best possible way to do it but this is how I did it,

 using UnityEngine.InputSystem;
   
 private void Awake() {
     inputAction = new PlayerInputActions()
     inputAction.PlayerControls.Jump.performed += Jump();
 }
 
 private void Jump() {
     // Function that makes the player jump
 }
  
 private void OnEnable() {
     inputAction.Enable();
 }
  
 private void OnDisable() {
     inputAction.Disable();
 }


Where PlayerInputActions is my input actions, PlayerControls is my action map and Jump is my jump action.

Edit: added in OnEnable() and OnDisable() functions that are also required.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chromodyne · Jul 12, 2020 at 10:46 PM 0
Share

Hmm, I think I follow but I'm not able to reference my Action $$anonymous$$ap like you are.

I did something along these lines:

 using UnityEngine.InputSystem;
 
 PlayerInput inputActions;
 
 private void Awake() {
 
     inputActions = new PlayerInput();
     //This line won't work.
 }

I tried serializing inputActions too. I'm still quite a beginner with Unity so I may be misunderstanding something simple.

avatar image finnjwohner Chromodyne · Jul 12, 2020 at 11:23 PM 0
Share

Ahh my bad, forgot the OnEnable() and OnDisable() functions that are also required. Altogether should look like this.

 using UnityEngine.InputSystem;
  
 private void Awake() {
     inputAction = new PlayerInputActions()
     inputAction.PlayerControls.Jump.performed += Jump();
 }
 
 private void Jump() {
     // Function that makes the player jump
 }
 
 private void OnEnable() {
     inputAction.Enable();
 }
 
 private void OnDisable() {
     inputAction.Disable();
 }
avatar image Chromodyne finnjwohner · Jul 12, 2020 at 11:44 PM 0
Share

Still not working properly. Testing out some other stuff but no luck.

What cached reference are you using for inputAction?

avatar image
0

Answer by bsgbryan · Jul 14, 2020 at 12:34 AM

Using the new Input system with DOTS

I have a Jump action defined in my PlayerInputActions asset. This Jump action has an action type of Button. It has two bindings: Space (for the Keyboard & Mouse control scheme) and Button South (for the Gamepad control scheme).

I then have the following System:

 using UnityEngine;
 
 using Unity.Entities;
 using Unity.Jobs;
 using UnityEngine.InputSystem;

 // PlayerInputActions is the name of my Input asset
 // Generated Input asset classes include an interface; making it easy to
 // handle all the actions you created in code
 public class GatherInput : SystemBase, PlayerInputActions.IPlayerActions {
 
   private PlayerInputActions input;
   private float  jumping = 0;

   // This is the handler for my Jump action
   public void OnJump(InputAction.CallbackContext context) {
     jumping = context.ReadValue<float>();
   }
 
   protected override void OnCreate() {
     input = new PlayerInputActions();

     // This registers this class as a handler for input events
     // For every action you define you need an appropriate OnAction
     // method in this class
     input.Player.SetCallbacks(this);
   }
 
   protected override void OnStartRunning() => input.Enable();
   protected override void OnStopRunning() => input.Disable();

   // NOTE: Multiple input events can happen between executions of OnUpdate.
   // This means that you need to be sure you sum up the data from all events
   // between OnUpdate invocations for things like Delta bindings
   protected override void OnUpdate() {    
     float j = jumping;
 
     Entities.ForEach((ref PlayerInputData input) => {
       input.Jumping  = j;
     }).Schedule();

     // Resetting this value allows the player to press jump again
     // NOTE: This means the player can spam the jump button
     // Some sort of timeout would be nice if you wanted to do
     // support double/triple jumps
     jumping = 0;
   }
 }

PlayerInputData is a component:

 using System;
 using Unity.Entities;

 using UnityEngine;
 
 [Serializable]
 [GenerateAuthoringComponent]
 public struct PlayerInputData : IComponentData {
   [HideInInspector]
   public float Jumping;
   public float JumpHeight;
 }
 

This component is added to the player GameObject. I use sub-scenes, so my player GameObject gets converted to an entity automatically. If you're not using sub-scenes, just make sure to add a ConvertToEntity component (you'll likely want to specify Convert and Destroy as the Conversion Mode) to your player GameObject.


I then have a PlayerJump system:

 using Unity.Entities;
 using Unity.Physics;
 
 public class PlayerJump : SystemBase {
   protected override void OnUpdate() {
     Entities.ForEach((
       ref PhysicsVelocity velocity,
       in PlayerInputData input
     ) => {
       velocity.Linear.y += input.Jumping * input.JumpHeight;
     }).Schedule();
   }
 }
 

This system causes the player to jump. If you don't want to use physics, you could animate the player's Translation.Value.y until it reaches input.JumpHeight.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by kingdom216 · Oct 20, 2021 at 04:58 AM

The problem that I am having is that jump is not defined by my input system, but I have set it up just the same as I did with move and run which work perfectly. Does anybody know how to fix this? alt text


playerinput.png (23.4 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What's the order of execusion for input events? 0 Answers

How to Detect Swipes using the VRInput Script 0 Answers

swimming ruins jumping 0 Answers

New Input System and how to use it 0 Answers

Input - is it OK to use SWITCH? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges