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 ardizzle · May 24, 2017 at 12:02 AM · velocityphysics2dbounce

Velocity.normalize to a min of one

Hello, I am working on a game with a ball that bounces around a fixed area. I am using this: GetComponent<Rigidbody2D>().velocity = 3 * GetComponent<Rigidbody2D>().velocity.normalized; to keep the ball moving at the same speed but able to change angles. The problem I am running into is that the X or Y can end up as 0 thus the ball stops moving up and down or left and right.

So what I believe is going on is that that line of code will lock the velocity to of X and Y to a sum of 3. thus meaning X can be 0 and Y can be three. So my question is how can I modify this to have it lock between 1 and 3 sum rather than 0 and 3 sum?

Any and all help is greatly appreciated.

Comment
Add comment · Show 7
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 sleepandpancakes · May 24, 2017 at 01:23 AM 0
Share

Normalizing a vector makes its length equal 1. This does not necessarily mean that the sum of the x and y components equal 1, rather that x^2 + y^2 = 1. Do you actually want to clamp the sum of the components or are you trying to clamp the length of the vector?

avatar image ardizzle sleepandpancakes · May 24, 2017 at 01:41 AM 0
Share

With what I am watching during the game play to see the values that are causing the issues is the X and the Y on the 2Drigidbody , under the Info Velocity section, that is attached to the ball. Uploaded a pic to help clarify. When these are both above the absolute value of 0 then it is working great. Otherwise it is giving me the issue as stated above with the no movement on the X or Y. alt text

2d-example.png (23.5 kB)
avatar image Zooow · May 24, 2017 at 02:39 AM 0
Share

i wonder if it's not because of (float) to (int) conversion. Have you tried with 3.0f ins$$anonymous$$d of 3 ?

avatar image ardizzle Zooow · May 24, 2017 at 02:44 AM 0
Share

I have tried that. And it has no issues with decimals. What is going on s that the normilized locks the number between 1 and 0. Then once X hits 0 then the ball just hits the top and bottom of the level so nothing has an effect on the X Velocity any longer.

avatar image Zooow ardizzle · May 24, 2017 at 02:59 AM 0
Share

I tested your line of code and it doesn't block the direction on x or y axis. I suspect it's another part of your script which has a bug. You surely has a "movement script" as your ball can't move if gravity scale = 0.

Show more comments
avatar image TonicMind · May 24, 2017 at 06:14 AM 0
Share

You ought to use Rigidbody2D.AddForce(force), and Rigidbody2D.AddTorque(torque) to control your object, ins$$anonymous$$d of altering the velocity yourself. Doing so can have unintended consequences. Like forcing the physics engine to recompute everything, which is expensive.

Additionally using GetComponent<TYPE>() all the time is prone to problems. Its very inefficient to use this every update (especially twice in one line). Also if the gameobject doesn't have a Rigidbody2D component, you will get a NullReferenceException and crash your game. Try caching (saving) that reference. Declare a variable Rigidbody2D my2DRB; in your class, and in Start() {my2DRB = GetComponent<Rigidbody2D>(); } or make that variable public and set it in the inspector.

I ought to have addressed this earlier. However I am not sure what your question is.

You want the ball to bounce around between two walls? Like in Pong? I understand your code is multiplying the normalized velocity by three, and assigning it to the same Rigidbody2D.

I'm not sure how 2D rigidbodies compute velocity but it must be similar to 3D RB's. You can set the drag higher to reduce the maximum speed of your object with a given force (or torque). The higher the drag, the slower the object.

Edit: your comment picture shows that you have the Z Axis constrained. That will prevent the object from rotating. Which means you won't have movement on any other axis, provided your object is facing along the Y axis. Try unchecking that box.

3 Replies

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

Answer by Bunny83 · May 24, 2017 at 11:02 AM

If both axis should have a minimum absolute value of 1 that limits the range of each axis to [-2.828, -1] and [1. 2,828] if you want to enforce a total velocity of 3:

VelocityLimits

To achieve that you want to first normalize the vector the way you did and in addition you would check each component and clamp them appropriately:

 Vector2 vec = rb2d.velocity.normalized * 3f;
 
 if (Mathf.Abs(vec.x) < 1f)
     vec = new Vector2(Mathf.Sign(vec.x)*1f, Mathf.Sign(vec.y) * Mathf.Sqrt(3f*3f - 1f*1f));
 else if (Mathf.Abs(vec.y) < 1f)
     vec = new Vector2(Mathf.Sign(vec.x) * Mathf.Sqrt(3f*3f-1f*1f), Mathf.Sign(vec.y)*1f);
 
 rb2d.velocity = vec;

Of course the value Mathf.Sqrt(3*3-1*1) is a constant (Mathf.Sqrt(8)) based on your two parameters. So you could pre-calculate it as it never changes.

 static float velocityLimit = Mathf.Sqrt(3f*3f - 1f*1f);

Note that if you want to use different "limits" you have to replace all "1f" constants with your desired minimum limit and "3f" with your desired total vector length.


velocitylimits.png (2.3 kB)
Comment
Add comment · Show 1 · 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 ardizzle · May 26, 2017 at 12:19 AM 0
Share

This is exactly what I was needing. Thank you man.

avatar image
0

Answer by Zooow · May 24, 2017 at 07:46 AM

Velocity is direction and speed expressed as a vector. If you don't allow your velocity vector to have x = 0, you'll never have your ball going straight up for instance, which breaks the purpose of physic controlled movement.

But your line of code seems not to be the problem for me. I think your problem is elsewhere in your code. Could you share more of your code?

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
0

Answer by plindsey · May 24, 2017 at 07:47 AM

But it wouldn't know if it should be 1 or -1 of there is no value in an axis? Why not add a little bit of chaos to stop it becoming stuck in left/right or up/down? Something like

 GetComponent<Rigidbody2D>().velocity = 3 * GetComponent<Rigidbody2D>().velocity.normalized + Vector2((Random.value * 0.2f) - 0.1f, (Random.value * 0.2f) - 0.1f);
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

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

70 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

Related Questions

Unity 2D - How do I add the velocity and direction of an object to another object? 2 Answers

how to make an 2d object move the direction its facing using rigidbody2d.velcoity 1 Answer

how to jump at fixed height but faster 0 Answers

Why is enemy jumping at different speed even though the velocity is the same? 0 Answers

.normalized sometimes returns 0 values, in a condition that only functions if value is not 0... 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