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 falconstrike209 · Aug 28, 2021 at 09:30 PM · inputunity 2djoystickmousepositiondash

How do I get the Vector2 from an input action in code? (New Input System)

I'm trying to make a dash ability that dashes in the direction of the mouse or in the direction that the controller knob is being held. I set up the input, and set the action type to value with control type as Vector2, I also set up the logic for how my dash will work. The logic should work fine, but I don't actually know how to get the Vector2 from the input to use in the dash. Here's the code, if you can help me that would be awesome. Thanks!

 public void Dash(InputAction.CallbackContext context)
     {
 
         //dash when dash button is pressed
 
         if (context.performed && canDash == true)
         {
             //stuff here to actually dash
             canDash = false;
             StartCoroutine(DashTime());
         }
     }
 
     public void DashAim(???)
     {
         //get vector2 from input (if that's how it works)
     }
 
     IEnumerator DashTime()
     {
         yield return new WaitForSeconds(3f);
 
         canDash = true;
     }
 
 }
Comment
Add comment · Show 1
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 falconstrike209 · Aug 29, 2021 at 09:17 PM 0
Share

I still need help with this, if anyone can help me please check this out

1 Reply

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

Answer by andrew-lukasik · Aug 29, 2021 at 09:33 PM


 public void DashAim ( InputAction.CallbackContext ctx )
 {
     Vector2 vec = ctx.ReadValue<Vector2>();
 }

Longer answer:

 public class DasherThing : MonoBehaviour
 {
     Controls _controls;
     float _lastDashTime = float.MinValue;
     float _dashDelay = 3f;
     public bool CanDash => Time.time > _lastDashTime+_dashDelay;
     void Awake ()
     {
         _controls = new Controls();
         _controls.player.Enable();
 
         _controls.player.dash.performed += Dash;
     }
     void OnDestroy ()
     {
         _controls.Dispose();
     }
     public void Dash ( InputAction.CallbackContext ctx )
     {
         // dash when dash button is pressed
         if( CanDash==true )
         {
             // update dash time:
             _lastDashTime = Time.time;
 
             // read inputs:
             Vector2 aim = _controls.player.aim.ReadValue<Vector2>();
 
             // execute a dash:
             
         }
         else Debug.Log("dash is not ready, communicate that with sound here");
     }
 }

Controls is a placeholder name for c# class name generated from your input action assets


Comment
Add comment · Show 11 · 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 falconstrike209 · Aug 29, 2021 at 09:37 PM 0
Share

Thank you, now I know how to get the input. Do you know how to reference that input in another void, now that I have it?

avatar image andrew-lukasik falconstrike209 · Aug 30, 2021 at 08:36 AM 0
Share

Read it straight from the input system there:

  Vector2 aim = _controls.player.aim.ReadValue<Vector2>();

Also, you're organizing your code for this in the wrong/messy way (coroutine is not needed). I expanded my original answer with a rough example.

avatar image falconstrike209 andrew-lukasik · Aug 30, 2021 at 01:42 PM 0
Share

hmm, I tried to do it more like your example, but for some reason the "Controls _controls;" at the beginning immediately creates an error. it says "The type or namespace name 'Controls' could not be found (are you missing a using directive or an assembly reference?)" I have "using UnityEngine.InputSystem;". Do I need something else as well?

Show more comments

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

168 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 avatar image avatar image

Related Questions

How do I use controller knob input like mouse position? (New Input System) 1 Answer

dash towards mouse position? (New Input System) 2 Answers

Input.mouseposition is confused by multiple clicks 1 Answer

Need Help with MousePos and dashing 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 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