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 Paliandro · Sep 18, 2021 at 03:33 PM · inputbuttonsdetectionreleasecancel

Detect the end of input with the new input system

I'm trying to make a system with the new input system, so that the player character accelerates while he is being moved, and the speed resets when he stops moving. Currently my code does manage to both move the player and enable the acceleration, but it doesn't detect the end of the player moving the character and disable the acceleration. Help with how to detect the end of input with the new input system?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class PlayerMover : MonoBehaviour
 {
     private PlayerControls playerControls;
 
     public float moveSpeed = 50f;
     public float maxSpeed = 100f;
     public float acc = 10f;
     public float baseSpeed = 50f;
 
     private Rigidbody2D rb;
     public bool accOn = false; //whether to accelerate movement or not
 
 
     private void Awake()
     {
         playerControls = new PlayerControls();
         rb = GetComponent<Rigidbody2D>();
 
         playerControls.Gameplay.Move.performed += Accelerate; //player is moving character
         playerControls.Gameplay.Move.canceled += Reset; //player stops moving character
     }
 
     private void OnEnable()
     {
         playerControls.Enable();
     }
 
     private void OnDisable()
     {
         playerControls.Disable();
     }
 
 
     void Accelerate(InputAction.CallbackContext context)
     {
         accOn = true;
     }
 
      void Reset(InputAction.CallbackContext context)
     {
         accOn = false;
     }
 
     void FixedUpdate()
     {
         Vector2 movementInput = playerControls.Gameplay.Move.ReadValue<Vector2>();
 
         if (accOn==true)
         {
             moveSpeed += acc * Time.deltaTime;
         }
         else
         {
             moveSpeed = baseSpeed;
         }
 
         rb.velocity = movementInput * moveSpeed * Time.deltaTime;
 
         
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ricky_lee_ · Sep 18, 2021 at 05:15 PM

The context variable contains information that you can use to determine if an input has been released, it also shows you when an input is initially pressed as well as if you are holding it down.


This context information can be seen as a bool in this way:

 context.started // True when the button is initially pressed 
 
 context.performed // True while if you are holding the button down
 
 context.canceled // True when the button gets released

Because I don't know exactly how your Accelerate() or Reset() are being triggered, I'm going to just assume that your Accelerate() is triggered by pressing 'W' or something.

So I've changed your Accelerate() to include an if() for all three of these contexts, just so you can see how and when they are true/false.

After playing with it hopefully you can see how to use this to fix your problem :)

     void Accelerate(InputAction.CallbackContext context)
     {
         accON = true;
 
         if (context.started)
         {
             Debug.Log("Initial button press");
         }
 
         if (context.performed)
         {
             Debug.Log("Button is being pressed");
         }
 
         if (context.canceled)
         {
             Debug.Log("Button has been released");
         }
     }




edit 1: Context contains a lot more than just the three pieces of information I've shown. The way I worded my answer is as if there are only these three pieces of information. All of them are listed here


edit 2:

Just to try and help you further, I'm guessing that your accON becomes true but then never sets to false?

If this is the case you would just need to do something like this, continuing off what I said before:

      void Accelerate(InputAction.CallbackContext context)
      {
          if (context.started)
          {
              accON = true;
          }
 
          if (context.canceled)
          {
              accON = false;
          }
      }


Or for an even cleaner looking way to do the same thing, using context.performed instead:

     void Accelerate(InputAction.CallbackContext context)
     {
         accON = context.performed;
     }


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

161 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

Related Questions

Gamepad input, axis OR button 0 Answers

Keep current rotation? 0 Answers

Input.GetKeyDown doesn't work with several keys 1 Answer

Mobile Button input broken when built for Mac/PC? 0 Answers

Larger phone sizes cant detect input 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