Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Gvrv · Jan 11, 2017 at 09:16 AM · addforceconstraintsmagnitudespeed up

Constraint an object speed

Hello,

I don't want my ball goes at a greater speed as I set in "AddForce()".

Is this code correct ?

     void Update()
     {
         if (ball.GetComponent<Rigidbody>().velocity.magnitude > 250.0f)
         {
             ball.GetComponent<Rigidbody>().SetMaxAngularVelocity(250.0f);
         }
     }
Comment
Add comment · Show 1
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 RobAnthem · Jan 11, 2017 at 09:25 AM -1
Share
      void Update()
      {
          if (ball.GetComponent<Rigidbody>().velocity > maxVelocity)
          {
              ball.GetComponent<Rigidbody>().velocity = maxVelocity;
          }
      }

2 Replies

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

Answer by LK84 · Jan 11, 2017 at 09:39 AM

SetMaxAngularVelocity only constraints the rotational velocity. I assume you want to constraint the translational velocity since you state this in your if-condition? And: Put in in the FixedUpdate() loop!

To constrain the translational velocity you got two options:

  1. The non-physics way (I don't recommend because you gonna mess with the physics engine)

  2. The physics way which probably takes a little more time to get exactly what you want

For (1) You could write something like this:

   const float maxSpeed=250;  
   void Start()
     {
     rigidbody=GetComponent<Rigidbody>();
     }
     void FixedUpdate()
     {
                  if(rigidbody.velocity.magnitude > maxSpeed)
                  {
                         rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
                  }
     }

For (2) you can either experiment with the drag value of your rigidbody in the inspector or write your own function, something like this:

 rigidbody.AddForce(-rigidbody.velocity*dampC); //dampfC is a damping Coefficient you can define yourself










Comment
Add comment · Show 2 · 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 Gvrv · Jan 11, 2017 at 09:51 AM 0
Share

Thanks !

I tried the first way, it works like a charm :)

avatar image LK84 Gvrv · Jan 11, 2017 at 09:53 AM 0
Share

ok, but be careful using it. It's not the "physical correct way" to do it.

avatar image
1

Answer by tanoshimi · Jan 11, 2017 at 09:37 AM

You're muddling up velocity (position) with angularVelocity (rotation). Also you don't need to keep setting a maximum value every frame - you can just set it once and it will persist. You should also try to avoid using GetComponent in Update - it's expensive. Find the rigidbody once in Start and re-use the result from them on.

Try this instead:

 private Rigidbody rb;

 void Start() {
   rb = ball.GetComponent<Rigidbody>();
 }

 void Update()
  {
      if (rb.velocity.magnitude > 250.0f)
      {
          rb.velocity = rb.velocity.normalized * 250f;
      }
  }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

bullet wont move forward 2 Answers

Ball Speed is not increasing as per code 0 Answers

GameObject falls instead flying horizontally at max height 1 Answer

Bullet won't fly forward 0 Answers

Vector3 Help - Calculating velocity toward a point 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