Search Unity

Adding force to a Rigidbody

Discussion in 'Scripting' started by Mirage, Feb 23, 2010.

  1. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    I suppose this is a prety common problem when using AddRelativeForce but I'll ask anyway. I am using AddRelativeForce to move a rigidbody under the user's control. When the user alters the transform.rotate of this gameobject I want the force direction to Lerp to the new rotation. Think of pushing a cube across a table. Halfway across, the cube rotates 90 degrees left. I'm trying to get the force direction to Lerp to the new heading of the cube instead of continuing in the direction it origionated at.
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    But this is precisely why you'd use AddRelativeForce, it adds a force relative to the rigid body's own coordinate system. So if you apply a force using AddRelativeForce in the x-direction, then that body spins, the force will continue to be applied in that body's own x-direction and that should cover you in this case.

    Are you not seeing that happen on your end?
     
  3. Mirage

    Mirage

    Joined:
    Jan 13, 2010
    Posts:
    230
    Yes, I understand that. I dont have a problem with the "additional" force, that is, the force being added. What is giving me problems is the "existing" force, the force that is already acting on the object. When I use add relative force, then turn it off instantly, there is still force "left over" that is acting on the object. This "left over" force is what I am trying to lerp to the new heading.
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    But unless you call a function to add a force there is no force being added to the body other than gravity and/or friction/drag. Perhaps you have a terminology issue here as what you seem to be describing is the fact that the body has momentum (a velocity and some mass) that is carrying it forward. What you want is to steer, right? In that case just take some manual control of the rigid body's velocity vector (determine what it is in the body's relative coordinate space before any "twist/turn", then lerp it to the same relative value after the "twist/turn".