Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 drydrunkpig · Aug 05, 2015 at 11:56 PM · rigidbody2dvelocity

How do I limit rigidbody2d velocity?

I'm trying to limit my characters speed but nothing I try works. Here's my current code: #pragma strict

 var force : Vector2;
 var bike : Transform;
 var power : int;
 var brake : int;
 var maxForward : int;
 var trigfunction : Vector3;
 
 function Update () {
 trigfunction = bike.TransformDirection(Vector3.right);
 
 force.Set(trigfunction.x,trigfunction.y);
 
 if (GetComponent.<Rigidbody2D>().velocity.magnitude < maxForward)
     GetComponent.<Rigidbody2D>().AddForce(force);
 
 if (Input.GetKey(KeyCode.UpArrow))
 GetComponent.<Rigidbody2D>().AddForce(force*power);
 
 if (Input.GetKey(KeyCode.DownArrow))
 GetComponent.<Rigidbody2D>().AddForce(force*brake*-1);
 }

What's wrong?

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

Answer by RLin · Aug 06, 2015 at 01:28 AM

You can check the velocity with velocity.magnitude. If the velocity is too high, disable the forces on the object until it becomes low enough again.

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 Owen-Reynolds · Aug 06, 2015 at 05:22 PM 0
Share

That's sort of what lines 13 and 14 do (but then the later keyboard don't check this. Not sure why not.)

avatar image
0

Answer by Owen-Reynolds · Aug 06, 2015 at 05:19 PM

To manually force it to a speed, multiply "too fast" velocities by whatever fraction you need to reduce it to the maximum. For example:

 float curSpeed = vel.magnitude;
 if(curspeed>maxSpeed) {
   float reduction=maxSpeed/curSpeed;
   vel *= reduction;
 }

Suppose the max speed is 5, and you're going 6 now. That code would multiply the entire xyz velocity by 5/6ths. Which really does reduce the speed by 5/6ths, to give a speed of 5 in whichever direction. It's a common Vector trick.

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 BomboBombom · Apr 27, 2016 at 02:36 PM 0
Share

It doesn't work very well, the rigidbody2d objects still hit speeds way above my speed limit.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

,Rotate a gameobject around another while being attracted by its gravity 1 Answer

Modifying a kinematic rigidbody2D.velocity makes player to go through the wall 0 Answers

rigidbody2D maximum y velocity 1 Answer

Rigidbody2D velocity in random direction with exceptions 1 Answer

How do I make my character's movement less floaty? 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