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 · Aug 31, 2021 at 11:06 PM · inputmousepositiondash

dash towards mouse position? (New Input System)

I am making a multiplayer game, where a main mechanic is the ability to dash towards the mouse. After quite a bit of pain, I have worked out all of the inputs. Now here's the issue: I don't know how to actually make the player dash. I have the button to start the dash, the timer for the dash, and the mouse/controller input set up. I know how to use things like "rb.velocity" or "rb.AddForce" But, I don't know how to take my input's Vector2 and actually apply it to the dash ability, to control the direction. Does anyone know how I might do this? Here's the code I currently have:

     public void Dash(InputAction.CallbackContext context)
     {
         // dash when dash button is pressed (dash button and the timer are handled in another void)
         if (startDash == true)
         {
 
             Vector2 DashAim = context.ReadValue<Vector2>();
 
             //dash
 
             startDash = false;
 
         }
 
     }
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

2 Replies

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

Answer by homer_3 · Sep 01, 2021 at 03:17 PM

Is your game 3D? If so, do camera.screenpointtoray on dashaim, where camera is your main camera. Use a physics raycast on that result to cast a ray into your scene. The hit result's point will give you the point you want to dash towards. You can do (point - player position).normalized to get the direction you want to dash in.

If it's a 2D game, you can just do (dashaim - player position).normalized to get the direction.

Comment
Add comment · Show 12 · 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 01, 2021 at 03:25 PM 0
Share

I have it set to normalize the Vector2 inside of the input manager, so I won't need to do that in code. I'm just double checking to make sure I properly understand. It is 2D. I had written a little bit of code but checking it in the IDE i'm using showed it to have several errors. could you maybe give a code example of how I would do this in the context of what I have already?

avatar image homer_3 falconstrike209 · Sep 01, 2021 at 04:01 PM 0
Share

Having the input manager normalize the returned mouse position is not a good idea. You need to have the player position and the mouse position in the same coordinate system. Of course, you could convert the player position to a normalized value, by why add that extra work in to begin with? You should have the input manager return the raw screen position of the mouse.

I don't really use Unity 2D, so I would've thought your player position would be a 2D vector, but if it's still a 3D vector, then do

 Vector3 dashaim3 = dashaim;
 Vector3 dashDir3 = (dashaim3 - camera.WorldToScreenPoint(player.transform.position)).normalized;
 Vector2 dashDir = new Vector2(dashDir3.x, dashDir3.y);

Then you can use dashDir as the direction to dash in.

avatar image falconstrike209 homer_3 · Sep 01, 2021 at 04:07 PM 0
Share

ok, i'm going to try this:

     public void Dash(InputAction.CallbackContext context)
     {
         // dash when dash button is pressed
         if (startDash == true)
         {
 
             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);
 
             //dash
             rb.AddForce(DashDir * DashSpeed);
 
             cantAirJump = true;
             startDash = false;
 
         }
 
     }
Show more comments
avatar image falconstrike209 · Sep 01, 2021 at 07:21 PM 0
Share

I have a physics material2D with 0 friction applied to the player, so it’s not that. The problem happens if I try to dash in midair as well. Although I think it’s because of the way I’m handling movement. In update I am constantly setting the velocity to input multiplied by move speed. I am going to change it so that that is only running if there is input.

avatar image falconstrike209 · Sep 01, 2021 at 07:22 PM 0
Share

I will also make sure that it is disabled during the dash, so that it won’t interfere if dashing while holding the movement keys. Thank you for all the help!

avatar image
0

Answer by WI_ZzeR · Sep 01, 2021 at 03:34 PM

when you use rb.AddForce it asks you for a direction vector this direction is basically the vector going from your character to the mouse simply get the mouse global position and substract it to your player's position this will give you thatdirection vector, in case of a 2d game it will return a 2d vector, if not just add 0 as the Vector3's Z value. then you use AddForce as you know it

Comment
Add comment · Show 6 · 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 01, 2021 at 03:48 PM 0
Share

alright. after messing with some stuff I got this:

 Vector2 posInScreen = Camera.main.WorldToScreenPoint(transform.position);
     
 Vector2 DashAim = context.ReadValue();
     
 //dash
 rb.AddForce(DashAim - posInScreen * DashSpeed);

this isn't giving me any errors, but i'm going to check it in the project and see if it is working as expected.

avatar image falconstrike209 falconstrike209 · Sep 01, 2021 at 03:56 PM 0
Share

this does not work at all. it's giving me some really weird behavior.

here's a video clip:

weird dash bug

avatar image falconstrike209 falconstrike209 · Sep 01, 2021 at 03:57 PM 0
Share

the video clip is using the last bit of code I sent.

avatar image falconstrike209 · Sep 01, 2021 at 03:58 PM 0
Share

@WI_ZzeR are you still here?

avatar image WI_ZzeR falconstrike209 · Sep 02, 2021 at 02:07 AM 0
Share

Sorry I live far away from the United states and it was around 1am already, seems taht you figured out how to do it but to answer your question i think your variable DashAim is the issue here, what exactly does it do? rb.AddForce(posInScreen - player.transform.position * DashSpeed); your addforce should look like this.

avatar image falconstrike209 WI_ZzeR · Sep 02, 2021 at 01:21 PM 0
Share

it's alright, we got it sorted out. thanks though!

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

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

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

Can I fake the mouse/touch position relative to the real mouse/touch position? 0 Answers

Force update of mouse position with the new input system 3 Answers

Problem with Vector3 Distance and Mouse Input, 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