I created a simple script that allowed one Rigidbody object (AI/Enemy) to smoothly chase after the Rigidbody player. However, it has one problem; the ‘Enemy’ is able to push the player across the screen with no regard for physics, and the player is powerless to push back, not matter how much force is applied.
Current AI/‘Enemy’ script:
#pragma strict
var rb2D: Rigidbody2D;
var player : GameObject;
var playerPOS : Vector2;
var followVector : Vector2;
var selfPOS : Vector2;
function Start ()
{
rb2D = GetComponent.<Rigidbody2D>();
player = GameObject.Find("CellPlayer");
}
function FixedUpdate () {
playerPOS = player.transform.position;
selfPOS = transform.position;
followVector = Vector2.MoveTowards(selfPOS, playerPOS, 0.06);
rb2D.MovePosition(followVector);
}
The player’s Rigidbody is moved using AddForce, which allows other forces to act against it, such as drag and collisions. However, my current method of moving the ‘Enemy’ requires MovePosition, which appears to ignore weight, drag, collisions, and basically all other forces.
I tried several ways to have the ‘Enemy’ follow the player through AddForce, but getting it to work to the point where it’s smooth and reliable seems overly complicated.
Is there another form of movement that I missed that allows you to tell a Rigidbody object to move (or attempt moving) to a vector while preserving its physical properties? Or is there an easier way to accomplish this with AddForce?
Edit: This is for a 2D game on Unity 5.3.