Search Unity

c# - Call current speed of an object

Discussion in 'Scripting' started by bigboybifdick, Oct 20, 2014.

  1. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hi everyone,

    I've been trying to solve this issue for a while now... I've tested a lot of things but i never get close to the result i'm looking for.

    In my project the player GameObject is always affected by external forces (local gravity or forces added by the player). What i would like to do is to slow down the speed of the object. Is there a way to do that ?

    Here is my force code :
    Code (CSharp):
    1. players.rigidbody.AddRelativeForce (Vector3.back * thePower * 12);
    I've tried this :

    Code (CSharp):
    1. players.rigidbody.velocity = Vector3.zero;
    2. players.rigidbody.angularVelocity = Vector3.zero;
    That does the job, it stops the forces. But i'd like to slow them down. How can i do that ?

    Thanks a lot for your help,
     
  2. BobFinery

    BobFinery

    Joined:
    Oct 14, 2014
    Posts:
    46
    If you want to add a little drag to it you could try

    Vector3 xDragForce = -players.rigidbody.velocity*fDragFactor;
    players.rigidbody.AddForce(xDragForce);

    This will add a force against the current velocity.

    If you want to directly manipulate the velocity you can try:

    players.rigidbody.velocity = players.rigidbody.velocity*fBrakeFactor;