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 Funderful · Feb 18, 2017 at 03:12 AM · c#player movementcuberigidbody.addforcespeed issues

Stop and object from moving faster after a specific speed is reached?

(I use C#. (The master race of code.))

So, i've making another simple game since it's been awhile since i've used Unity. And to put it simply, I'm actually having fun with it! :D However, I've searched the Documentation and Scripting API for a while now, and I still haven't found a real good answer for a little problem I have. Maybe one of you coding deities could lend me a hand, or at least point me to a webpage, that would be awesome!.

This is the situation. I have a little cube named "Player", and it moves forward at a set speed. I made the script in the only way I know how (because I'm still a n00b at C#), which is by getting the Rigidbody of the player and using Rigidbody.AddForce with the FixedUpdate function (I've been told that using FixedUpdate is good for physics code). The problem is, the cube lands on the ground, speeds up, but never slows down, until it reaches light-speed. For my game to actually be playable, I need the cube to just stop moving so fast and stop speeding up once it has reached a certain velocity, and stay at that velocity.

PS. This isn't exactly necessary, but it would be neat to use. If any of you know how to disable a function, in my case my update function for movement, once a bool becomes true, that would be great to know as well.

Here's the code :

 public class PlayerMovement : MonoBehaviour {
 
 
 
     public Rigidbody rb;
     public int playerMovementSpeed;
     public int forwardSpeed;
 
     // Update is called once per frame
     void FixedUpdate () {
         rb.AddForce (0, 0, forwardSpeed);
         if (Input.GetKey (KeyCode.D)) {
             rb.AddForce (playerMovementSpeed, 0, 0);
             } 
         else if (Input.GetKey (KeyCode.A)) {
             rb.AddForce (-playerMovementSpeed, 0, 0);
             } 
             // I want to put my " Stop-speeding up " code here.
         }
     }
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
3
Best Answer

Answer by Bunny83 · Feb 18, 2017 at 10:54 AM

You can simply add this line at the end of your FixedUpdate method:

 rb.velocity = Vector3.ClampMagnitude(rb.velocity, MaxVelocity);

Of course "MaxVelocity" is a float value indicating your max velocity. Note that it clamps the absolute velocity and not on a "per axis" basis. This is usually what you want. It prevents the user from gaining speed by running forward and strafing left or right. Though it also takes the y-speed into account. If you want to ignore the y-speed and only clamp the x-z-speed you can do:

 Vector3 v = rb.velocity;
 v.y = 0;
 v = Vector3.ClampMagnitude(v, MaxVelocity);
 v.y = rb.velocity.y;
 rb.velocity = v;

Or without ClampMagnitude:

 Vector3 v = rb.velocity;
 float speed = Mathf.Sqrt(v.x*v.x + v.z*v.z);
 v.x /= speed; // normalize x-z-speed vector
 v.z /= speed;
 if (speed > MaxVelocity) // clamp the current speed
     speed = MaxVelocity;
 v.x *= speed; // scale the x-z-speed by the clamped speed
 v.z *= speed;
 rb.velocity = v;

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 Funderful · Feb 18, 2017 at 06:19 PM 0
Share

Thanks! :D

avatar image
0

Answer by aFeesh · Feb 18, 2017 at 03:43 AM

You can add an if check on the rigidbodies velocity and if it exceeds your 'max speed' then don't add any more force.

As for 'disabling' the Update function you can just have a bool check to return or not like so:

 private bool isUpdateDisabled = false;
 
 void FixedUpdate () {
      if (isUpdateDisabled){
           return;
      }
 }

So when isUpdateDisabled is true the function will return immediately and not execute anything below it.

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 Funderful · Feb 18, 2017 at 06:20 PM 0
Share

Yours works too. Thanks guys!

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

283 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 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 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 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 avatar image avatar image avatar image

Related Questions

When stopping Rigidbody.velocity 1 frame stutter C# 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

When player hit wall continue moving 0 Answers

highlight cube sides on mouse hover 2 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