Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 · Sep 03, 2021 at 05:33 PM · inputcontrollermousepositionvector2dash

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

I recently made a dash ability that dashes towards the mouse, using the new input system. I want this ability to also work if using a controller, by reading the direction the knob is being pushed and using that for the direction of the dash. Does anyone know how I can set it up so that the direction the knob is being pressed works like the direction to the mouse from the player?

I had tried this setup: alt text

but when dashing on the controller it would ignore the direction I was pressing the knob, and just dash downwards diagonally to the left, regardless of the mouse's position even. The part of the script I am using to read the input and dash looks like this:

     public void Dash(InputAction.CallbackContext context)
     {
 
         Vector2 dashAim = context.ReadValue<Vector2>();
 
         Vector3 dashAim3 = dashAim;
         Vector3 dashDir3 = (dashAim3 - camera.WorldToScreenPoint(rb.transform.position)).normalized;
         Vector2 dashDir = new Vector2(dashDir3.x, dashDir3.y);
 
         //if button has been pressed
         if (startDash == true)
         {
 
             //dash
             rb.velocity = dashDir * dashSpeed;
 
             cantAirJump = true;
             startDash = false;
 
         }
 
     }
capture.png (11.7 kB)
Comment
Add comment · Show 2
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 · Sep 03, 2021 at 05:34 PM 0
Share

@Hellium is this good enough for you?

avatar image Hellium falconstrike209 · Sep 03, 2021 at 05:36 PM 0
Share

Yes it is! ;)

1 Reply

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

Answer by Hellium · Sep 03, 2021 at 05:47 PM

I have barely used the Input System, but from what I understand, the Position [Mouse] entry will return the position of the mouse in the Screen Space ((0,0) → (Screen.width, Screen.height)) while the Left Stick [GamePad] certainly returns values between (-1,-1) → (1,1), so Vector3 dashDir3 = (dashAim3 - camera.WorldToScreenPoint(rb.transform.position)).normalized; does not make sense here.


I believe you will have to create two actions, a DashWithMouse and a DashWithStick action.


FOLLOWING CODE NOT TESTED

  // To be called instead of the Dash function when adding the listener
  public void ReadMouseInput(InputAction.CallbackContext context)
  {
 
      Vector2 mousePosition = context.ReadValue<Vector2>();
      Vector3 direction = (mousePosition - camera.WorldToScreenPoint(rb.transform.position)).normalized;
      Dash(new Vector2(direction.x, direction.y));
  }

  // Callback of the Left Stick
  public void ReadStick(InputAction.CallbackContext context)
  { 
      Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
      Dash(new Vector2(stickDirection.x, stickDirection.y));
  }

  public void Dash(Vector2 direction)
  {
      //if button has been pressed
      if (startDash == true)
      {
 
          //dash
          rb.velocity = direction * dashSpeed;
 
          cantAirJump = true;
          startDash = false; 
      } 
  }
Comment
Add comment · Show 2 · 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 · Sep 03, 2021 at 06:10 PM 0
Share

Works like a charm! Sorry I was being so stubborn about changing my script, I guess I was too incompetent in this area to know whether I would have to or not. Thank you for helping me, in spite of me being a jerk.

avatar image Hellium falconstrike209 · Sep 03, 2021 at 06:24 PM 0
Share

That's ok, I am glad the code worked and I appreciate you acknowledged your faults. Good luck!

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

How do I use an Input axis instead of Input.mousePosition??? 1 Answer

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

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

How do I rotate an object using Vector2 inputs? (New Input System) 1 Answer

valve index A button mapped as grip 1 Answer


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