Search Unity

Check current relative force

Discussion in 'Physics' started by XiCHR15iX, Aug 27, 2019.

  1. XiCHR15iX

    XiCHR15iX

    Joined:
    Nov 29, 2018
    Posts:
    1
    How do I check the current relative force of an object, e.g. speed a car is traveling?
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    118
    if we consider the speed as the amount of velocity in the forward direction, you could use a dot product

    Code (CSharp):
    1. speed = Vector3.Dot(rigidBody.velocity, transform.forward);
     
    XiCHR15iX likes this.
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    1,548
    Force and speed are different properties.
    • Speed is simply rigidbody.velocity.magnitude. Taking the forward projection would make the value oscillating largely if the car is not moving forward, i.e. drifting or sliding laterally.
    • As you're mixing "force" and "speed" in your question then you may be referring to the momentum, which is mass times velocity. The magnitude of the lineal momentum of the car is simply rigidbody.velocity.magnitude * rigidbody.mass.
     
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    118
    I'm not that level but i don't see how a simple calculated data could make an object drifting unless we use it in a drift formula. And we take the transform.right projection. here, its just a float...
    drifting could be obtained by :

    1. Calculating the sideway speed (A float )
    2. Calculating Side Friction (A vector3 function of the float above and the transform.right projection using Time.fixedDeltaTime and dividing it by a certain amount to have (or not) a drift/slide behaviour...
    3. Then Apply the calculated Side Friction as a Force. rigidBody.AddForce(sideFriction).
     
    Last edited: Aug 28, 2019