I’m making a Geometry Wars style game. I’m programming an enemy to move towards the player and start rotating around the player when the enemy is close enough. However, with my current code the enemy rotates in a triangle around a point in front of the player. Any idea what’s causing it?
{
public Transform player;
public Rigidbody2D rb;
public float enemySpd = 3;
void Start()
{
//Sets player to the transform of the player
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void FixedUpdate()
{
FollowPlayer();
}
void FollowPlayer()
{
//Moves the enemy towards the player
transform.right = player.position - transform.position;
//Gets the distance between the enemy and the player
float dist = Vector2.Distance(player.position, transform.position);
//Sets zAxis to the z axis for the enemy to rotate around
Vector3 zAxis = new Vector3(0,0,1);
//Sets vec to an X axis for the directions of the enemy to face
Vector2 vec = new Vector2(1, 0);
//If the enemy is far enough away have him move toward the player
if (dist > 3)
{
//Moves the enemy forward
rb.velocity = transform.TransformDirection(vec * enemySpd);
}
//If the enemy is close enough stop moving forward and rotate around the player
else
{
//Rotates around the Z axis of the player
transform.RotateAround(player.position, zAxis, 1);
}
}
}