I’m making a simple top down shooter to get to grips with Unity and have hit a snag.
When the player moves within range of an enemy, the enemy should start following the player. This works using the following in FixedUpdate:
function FixedUpdate()
{
if (player)
{
distance = Vector3.Distance( player.transform.position, transform.position);
if ( distance < 1.5 && distance > 0.05 )
{
var delta = player.transform.position - transform.position;
delta.Normalize();
var speed = moveSpeed * Time.deltaTime;
print("close");
rigidbody.MovePosition(rigidbody.position + (delta * speed));
}
}
}
However, It doesn’t work if I use:
rigidbody.AddForce(rigidbody.position + (delta * speed));
Furthermore, it doesn’t even work if I use:
rigidbody.AddForce(transform.forward * 10000,ForceMode.Impulse);
There’s just no movement at all when using AddForce. It’s probably something really simple but I couldn’t find an answer on the forums/online.
Has anyone encountered this before?