I have a kinematic rigidBody whose position i control using its transform.
There is a point in the script where i do this :
transform.position = Vector3.Lerp(transform.position, targetPosition, 60f);
I Was using this code before adding a rigidbody to the object, and it was working fine.
Now with the RB it doesn’t lerp at all, it goes straight to targetPosition.
Doesn’t Lerp work with rigidBody ?
60F signifies the end of a lerp. The final value of Lerp should always be between 0 and 1, where 0 is the start and 1 is the end. I’d be surprised if it worked before, to be quite honest. I did the same things before I understood Lerp and it always jumped to the end.
Try doing:
float _speedModifier = 2; // How fast you want the RB to lerp
float _allowedDistanceFromPos = 1; // How close you can be to the target position
Vector3 targetPosition;
Transform transform;
void Update()
{
if (Vector3.Distance(transform.position, targetPosition) > _allowedDistanceFromPos)
LerpRB();
else
_currMovementPos_Lerp = 0; // Resets the lerp when you're close enough
}
float _currMovementPos_Lerp = 0; // Holds a value to the current lerp "t" value.
void LerpRB()
{
_currMovementPos_Lerp += Time.deltaTime * _speedModifier; // Adds the speed at which you want to move.
transform.position = Vector3.Lerp(transform.position, targetPosition, _currMovementPos_Lerp);
}
That will give you much more desirable results. Just substitute your own fields, obviously.