I know that directly setting the position isn’t recommended and that usual way to move the npc would be using .AddForce() in the rigid body. But this NPC is constantly following the player arround, and only moves in straight lines, and I coundn’t get a satisfying result out of adding forces to the RigidBody. I settled on using Vector2.MoveTowards(), here is a simplified example of the code:
private Vector2 FollowPlayer()
{
if(playerDistance < followDistance)
{
return currentPosition;
}
else
{
return Vector2.MoveTowards(currentPosition, currentPosition + adjustedMovement, speed * Time.deltaTime);
}
}
Then on the Move() method of this NPC I constantly set his position to the value returned by FollowPlayer():
private void Move()
{
Vector2 targetPosition = FollowPlayer();
transform.position = targetPosition;
}
This is working, but the movement isn’t very smoth, is there a better way of doing this than using Vector2.MoveTowards() that would lead to smoother movement? Slowing his speed down works, but then he can’t keep up with the player. I would preffer to still directly set his position since it’s easier to control exaclty how he moves, but I’m open to any tips or advice.