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 /
  • Help Room /
avatar image
1
Question by iDifferent · Jun 30, 2016 at 05:54 PM · scripting problemmovement script

Diagonal movement is faster than horizontal or vertical, 2D top down, left click to move

so pretty much what the title says, it has been answered before but since I use the mouse to move the recommentations dont work on my script

 void FixedUpdate ()
 {
     
     GetComponent<Rigidbody2D> ().velocity = velocity;
     Vector3 mousePosition = Input.mousePosition;
     mousePosition.z = +1;
     if (Input.GetKey (KeyCode.Mouse0)) 
     {
         GetComponent<Rigidbody2D> ().freezeRotation = false;
         velocity = (Camera.main.ScreenToWorldPoint(mousePosition) - gameObject.transform.position) * Time.deltaTime * 100f;
         if (velocity.x >= MaxSpeed.x) 
         {
             velocity.x = MaxSpeed.x;
         }
         if (-velocity.x >= MaxSpeed.x) 
         {
             velocity.x = -MaxSpeed.x;
         }
         if (velocity.y >= MaxSpeed.y) 
         {
             velocity.y = MaxSpeed.y;
         }
         if (-velocity.y >= MaxSpeed.y) 
         {
             velocity.y = -MaxSpeed.y;
         }
     } 
     else 
     {
         GetComponent<Rigidbody2D> ().freezeRotation = true;
         velocity.x = 0f;
         velocity.y = 0f;
     }
 }
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
1
Best Answer

Answer by Thajocoth · Jun 30, 2016 at 09:02 PM

The problem is that you're using separate max speeds for X & Y, which gives you a max speed box instead of a max speed circle. You need an absolute max speed.

 const float maxSpeed = 10f;

 float pythagoras = ((velocity.x * velocity.x) + (velocity.y * velocity.y));
 if (pythagoras > (maxSpeed * maxSpeed))
 {
     float magnitude = sqrt(pythagoras);
     float multiplier = maxSpeed / magnitude;
     velocity.x *= multiplier;
     velocity.y *= multiplier;
 }
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 iDifferent · Jun 30, 2016 at 09:34 PM 1
Share

thanks a lot, this worked great, I never imagined I needed to use the pythagora's theory

avatar image Thajocoth iDifferent · Jul 01, 2016 at 12:03 AM 0
Share

I'm glad it helped!

Yeah, all the old math formulas come up now and then. For cartesian coordinates, Pythagoras comes up a lot, because the hypotenuse of the right triangle created by those cartesian coordinates is often useful. That's how you convert (x,y) to distance or (vx, vy) to speed.

Good luck with your game!

avatar image Derpy834N · Aug 14, 2021 at 08:45 AM 0
Share

Thank you for this. I'm learning to code in lockdown. And I just realized a lot of math lessons at school can be very useful.

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

69 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

Related Questions

Why isn't my player moving down? 0 Answers

Moving RigidBody Smoothly 0 Answers

GoogleVR / C# - How to, with the "pointer click": click 1 move a cube and click 2 move back the cube 0 Answers

Making a sword follow mouse movement 1 Answer

Stop movement on collision 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