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
0
Question by aceofpack · Feb 15, 2016 at 08:40 PM · speedvector2

Velocity X and Y speed limits

I've looked around and tried many of the answers but still can't quite get what I want. I have many gameObject prefabs with rigidBody2d.

I apply a force to get them moving in their physical world but things get out of hand if they go too fast from various collisions. I'm looking to have min and max speed limits for both the x and y velocities (which can be negative).

Ideally the objects should never be able to go horizontally across or vertically up/down because the speed limits will kick in and can't get stuck stationary.

I first tried:

         if(rb.velocity.magnitude > maxSpeed) {
             float reduction = maxSpeed/rb.velocity.magnitude;
             rb.velocity *= reduction;
         }
 
         if(rb.velocity.magnitude < minSpeed) {
             float increase = minSpeed/rb.velocity.magnitude;
             rb.velocity *= increase;
         }
 

But didn't work. Think vectors are beating my brain up. Thanks for any help.

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
0

Answer by Salmjak · Feb 15, 2016 at 08:50 PM

I suppose you already tried: rb.velocity.x/y = minSpeed/maxSpeed? Ex.

 if(rb.velocity.y < minSpeedY){
 Vector2 newVelocity = new Vector3(rigidbody.velocity.x, minSpeedY);
 rb.velocity = newVelocity;
 }

EDIT: Ok, let's do the math. What does Vector2.magnitude do? It gets the magnitude which is the same as the Hypotenuse of the corresponding triangle. What does multiplying a vector2 do? It multiplies each component by the factor.

 So you try to solve the equation sqrt(x*x + y*y)*q = minSpeed.
 Let's say minSpeed = 20 and our Vector is (2,1). This will give us the equation: sqrt(4)*q = 20. So q= 20/2 = 10.
 When you then multiply your vector you get a vector which is (20,10) the magnitude of this is sqrt(20*20 + 10*10) = sqrt(400 + 100) = sqrt(500) ~= 22.
 So you didn't get 20 as you wanted.
 What you really want to solve is: 20*20 = x*q*x*q + y*q*y*q.
 So if the vector2 was (2,1) this would get us 400 = 4q^2 + q^2 = 5*(q^2). So we would get q = sqrt(400/5) ~= 8.94.
 If we now multiply our vector we would get (17.88,8.94) and the magnitude of this would be sqrt(17.88^2 + 8.94^2) ~= sqrt(320 + 79.9) = sqrt(399.9) = 19.99 ~= 20!

Hurray!

So in your code the equation would be:

 float increase = Mathf.sqrt((minSpeed*minSpeed)/((rb.velocity.x*rb.velocity.x) +(rb.velocity.y*rb.velocity.y))); //We just break out q from our equation "20*20 = x*q*x*q + y*q*y*q" => "20^2 = x^2*q^2 + y^2*q^2" => "20^2 = (x^2 + y^2)*q^2" => sqrt(20^2/(x^2 + y^2)) = q

Comment
Add comment · Show 8 · 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 aceofpack · Feb 15, 2016 at 08:57 PM 0
Share

Yeah, it doesn't work because y can be negative too - so looking to limit ranges of, for example y can be -0.5 to -1.5 OR 0.5 to 1.5

avatar image Salmjak · Feb 15, 2016 at 09:49 PM 0
Share

I'm sure there is some function that I don't know about that does this for you, but there you go.

avatar image aceofpack · Feb 15, 2016 at 09:51 PM 0
Share

Thanks for the breakdown, I think I'll need to sleep on it as struggling to follow.

avatar image Salmjak aceofpack · Feb 15, 2016 at 09:53 PM 0
Share

Just copy the last code-snippet and see if it works. What it should do: when your velocity is greater/less than maxSpeed/$$anonymous$$Speed it should scale up/down the vector without changing the direction.

avatar image aceofpack · Feb 15, 2016 at 10:09 PM 0
Share

Like this?:

         if(rb.velocity.magnitude < $$anonymous$$Speed) {
             float increase = $$anonymous$$athf.Sqrt(($$anonymous$$Speed*$$anonymous$$Speed)/((rb.velocity.x*rb.velocity.x) +(rb.velocity.y*rb.velocity.y)));
             rb.velocity *= increase;
         }


avatar image Salmjak aceofpack · Feb 15, 2016 at 10:21 PM 0
Share

Yeah, exactly :)

avatar image aceofpack · Feb 15, 2016 at 10:52 PM 0
Share

I get this:

 Rigidbody2D.velocity assign attempt for 'blueBubble(Clone)' is not valid. Input velocity is { NaN, NaN }.
 UnityEngine.Rigidbody2D:set_velocity(Vector2)
 BlueBubble:FixedUpdate() (at Assets/Scripts/InGame/BlueBubble.cs:59)
 

I guess because 0's can enter the equation

avatar image Salmjak aceofpack · Feb 15, 2016 at 11:03 PM 0
Share

Yeah. I would also guess that. If both x and y vectors are 0 it would cause a division by zero. Try checking for zero (if(rb.velocity.x != 0 || rb.velocity.y != 0), so if one of them isn't zero you can continue).

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

43 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

Related Questions

How to revert speed back to normal after changing with Key. 1 Answer

C# movement code with the maximum movement speed. 1 Answer

Object jumping at high speed! 0 Answers

Can a csv file be used to change a player speed? 0 Answers

Virtual Joystick Player Speed Problem 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