Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do you make a character run at a specific speed?

Discussion in 'Scripting' started by vertig02, Feb 24, 2019.

  1. vertig02

    vertig02

    Joined:
    Nov 30, 2017
    Posts:
    61
    Been trying to come up with the solution for this and tried googling, but didn't really make any progress

    I know how to modify Character speed normally, be it through transform.Translate or Move Position where you multiply it by a scalar value of your liking, or even just being lazy and using the built in FPSController

    But what I'm supposed to do now is ensuring a character that is moved completely by physics (No animation or direct translation through script, rather moved purely by adding rigidbody forces, highly accurate simulation of real life physics and movement was emphasized in the current project) runs at a specified 10km\h or rather 2.8m\s on flat ground, though occasional fluctuations are forgiven especially if on rough terrain. Not really too sure how I can do this, would be great if someone could help :)
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    If you want to prevent acceleration of rigidbodies when they reach a certain speed, you can just stop adding force if the object is going over a certain speed. How you measure the speed can vary, but a lot of times the rigidbody velocity can be used to measure speed. If the velocity goes over a certain amount, don't let any more force be added. A nice way to do this more smoothly, is to reduce the amount of force added slowly, as the rigidbody approaches full speed - and while I am not sure if this math is right, you can do something similar to this:
    Code (CSharp):
    1.     Rigidbody rb; // this is the rigidbody on the plane
    2.     float currentSpeed; // the speed the plane is moving at currently
    3.     float maxSpeed = 100; // the max speed the plane should be able to accel
    4.     Vector3 defaultForce = new Vector3(0, 0, 50); // how much "power" the engine of the plane has to push it forwards, this value will need tweaked to fit your game
    5.     Vector3 forceToAdd = Vector3.zero;
    6.     private void FixedUpdate()
    7.     {
    8.         // here we get the current speed - might want to do some Debug.Log() statements and see how fast it gets going, before deciding what to set max speed to!
    9.         currentSpeed = rb.velocity.magnitude;
    10.         // here we are applying the forces to the rigidbody
    11.         if (currentSpeed > maxSpeed - (maxSpeed / 4))
    12.         {
    13.             // we are going fast enough to limit the speed
    14.             float forceMultiplier = defaultForce.z * maxSpeed - (currentSpeed / maxSpeed);
    15.             forceToAdd.z = forceMultiplier; // set the force on Z to be closer and closer to zero as speed increases
    16.         }
    17.         else //  we are not moving too fast, add full force
    18.         {
    19.             forceToAdd = defaultForce;
    20.         }
    21.         // now we actually add the force
    22.         rb.AddForce(forceToAdd);
    23.     }
    Remember that this is untested, and perhaps the math is wrong, but hopefully you get the idea of reducing the force added by some amount up until its adding zero force at max speed. Also, I wrote this example for a plane flying with forces, but the idea is the same, so disregard the comments talking about a plane :D
     
    vertig02 likes this.
  3. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    vertig02 likes this.
  4. sonali_31

    sonali_31

    Joined:
    Feb 11, 2019
    Posts:
    3
    Got to learn something new..!!!
     
    MD_Reptile likes this.
  5. vertig02

    vertig02

    Joined:
    Nov 30, 2017
    Posts:
    61
    I think I may have an idea, if I set Unity to have air and ground resistance forces as well, and define a max power for the propulsion (Max force character can push forward), then through trial and error I could find the specific max peed where both would cancel each other out?
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    That may work, give it a shot. I haven't tried doing that though so I can't say how tricky that is.
     
    vertig02 likes this.