Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ZozeR · Dec 22, 2019 at 02:07 PM · rigidbody2dphysics2dmathmathf

Limit mouse position so that player launches same distance

Player dashes towards the mouse position, however, lets say you put the cursor far-right like x(20) so player moves 20mt but if you put it x(5) player dashes 5mt how can I limit the pos. input so the player would travel the same distance no matter the mouse position distance.

I want to get the mouse direction and launch the player same distance no matter the mouse position distance.

     //this is inside the Fixed Update method.
     float xor = Input.GetAxis("Horizontal"), yor = Input.GetAxis("Vertical");
 
     if ((xor != 0 || yor != 0) && !dash)
     {
         rg2d.velocity = new Vector2(xor * speed, yor * speed);
         anim.SetFloat("speed", Mathf.Abs(xor + yor));
     }
     if(Input.GetKeyDown(KeyCode.Mouse0) && !fire)
     {
         //PUNCH
         dash = true;
         fire = true;
         StartCoroutine(Stop_Fire(0.5f));
         anim.SetTrigger("attack");
         fist.GetComponent<Collider2D>().enabled = true;
 
         Vector2 pos = (transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)).normalized;
 
         rg2d.AddForce(-pos * punch_force, ForceMode2D.Impulse);
     }
 
 //I call this function at the end of the attack animation.in the Animation window
 public void Stop_Dash()
 {
     dash = false;
     fire = false;
     fist.GetComponent<Collider2D>().enabled = false;
     rg2d.velocity = Vector2.zero;
 }
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 ZozeR · Dec 24, 2019 at 12:34 AM

The problem was the mouse input z-axis. it was unused but still had a big value that effected the normalization.

instead of :

 Vector2 pos = (transform.position Camera.main.ScreenToWorldPoint(Input.mousePosition)).normalized;

this should be used:

 Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 Vector3 moveDirection = (transform.position - mousepos);
 moveDirection.z = 0;
 moveDirection.Normalize();
 
 rg2d.AddForce(-moveDirection * punch_force, ForceMode2D.Impulse);
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
1

Answer by Captain_Pineapple · Dec 22, 2019 at 03:20 PM

well clamping is not the right solution that is correct. Try the following line instead:

 Vector2 pos = transform. position + (transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition)).normalized * yourMaximumDashDistance;

you will probably have to adjust some values ot get the right distance but this should result in a "clamped relative position" to your character.


Share the result :)

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 ZozeR · Dec 22, 2019 at 03:50 PM 0
Share

i tried changing the value but sadly this does not work, is there a way to only get the direction of the mouse and add force towards that direction regardless of its distance.

avatar image Captain_Pineapple ZozeR · Dec 22, 2019 at 09:36 PM 0
Share

Oh well i just realised you have some other big issue in your current code:


GetAxis will never be true for just one Update....

So depending on how long you click the result will be different... You should rather choose something like GetButtonDown to have a single even or follow this answer here...


let me know if that solved your issue.

avatar image ZozeR Captain_Pineapple · Dec 23, 2019 at 06:08 PM 0
Share

i edited the code to show what i changed but still player launches depending on the mouse distance

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

131 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

Related Questions

2D AddForce towards direction with CONSTANT FORCE whether the direction is far or near. 1 Answer

How to create an Asteroids style moving in 2D mode? 2 Answers

Workarounds for Higher Control When Using Physics2D? 0 Answers

Rigidbody2D freezes 1 Answer

Rigidbody2D is kind of flying instead of falling when the child box is colliding 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