Hey, I am having a problem with a 2d (air)plane I am working on. It is a side scrolling game, and I want the plane to follow the player. To move I am using the rigidbody2D and to rotate I am using Quaternion LookAt. The problem is, when the aircraft exceeds the X position of the player, it goes in the opposite direction, so it's moving away from the player instead of towards it. When it gets behind the player again ( X<=player X), it follows the player normally.
void FixedUpdate ()
{
Rotate ();
Move ();
}
void Rotate()
{
Quaternion newRotation = Quaternion.LookRotation (player.transform.position - transform.position);
Quaternion newRot = Quaternion.Slerp(transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
transform.rotation = new Quaternion(0, 0, newRot.z, newRot.w);
}
void Move()
{
rigidbody2D.AddForce (transform.right * flySpeed*8 * Time.deltaTime, ForceMode2D.Impulse);
if(rigidbody2D.velocity.magnitude > flySpeed)
{
rigidbody2D.velocity = rigidbody2D.velocity.normalized * flySpeed;
}
}
I want the aircraft to rotate with a rotatespeed, so it won't instantly face the player, and has to make a nice loop when it needs to.
Can anyone help me with this? I've looked into multiple solutions, but haven't found anything.
↧