Hi,
So RB should be moved in FixedUpdate. Is this the case whether it is moved by AddForce, or MovePosition, or both?
My RB is following a NavMesh waypoint path. What I’m trying to do is move my RB a shunt to its right or left on press of a key:
public float shuntTimer, shuntForce;
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
if (shuntTimer>0) // trigger in Update by virtue of the timer != 0
{
// apply a force with magnitude shuntForce; cease the force when the timer expires
rigidbody.AddForce(transform.right*shuntForce*shuntTimer);
// reduce the timer and also the duration of the force
shuntTimer -= Time.deltaTime;
}
}
So this doesn’t work. Not sure why not.
Vector3 velocity;
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
velocity = Vector3.Lerp(velocity, transform.right*shuntFactor, Time.deltaTime);
}
}
void FixedUpdate()
{
rigidbody.MovePosition(rigidbody.position + velocity * Time.deltaTime)
}
And this doesn’t either. Again, not quite sure why not.
When I say “doesn’t work”, neither of these approaches moves the RB at all.
Any help please?