Follower Airplane problem

Hi,

i made a script for controlling an airplane (and the entire airplane’s behaviour) two days ago, just to see if i could.
Then, i figured it would be fun to have another tiny drone following me around.

I copied the entire script from my airplane, and instead of controlling it with keys, i wanted it to check how it should move by comparing it’s rotation and position to mine.

Everything went fine, untill i did the y rotation. If it’s rotation was 10, and the direction to my plane was 350, it would turn 340 degrees to the right instead of the (for humans) logical 20 degrees to the left.

I tried a few things to fix that, but was unsuccessful. When looking for other solutions, i found this website, and decided to ask for help here.

Here is the entire code for my drone follower! (C#!)
(sorry for for the uncommented mess beneath, never thought i would have to show it to others. If you want to help, but don’t know what some of the variables are supposed to do, feel free to ask :stuck_out_tongue: (some of the declared variables aren’t even used, for example.))

Also, my e-mail is timfalken@hotmail.com!

125**------- Le Code ------**125

using UnityEngine;

using System.Collections;

public class FollowerDrone : MonoBehaviour {

public float snelheid;

float stuur = 0f;

float stijging = 0f;

public float gravity = 0f;

public float throttle = 1f;

float floatheight =0.01f;

bool floatup = true;

//AI STUFF//



public bool Fly = true;

int steer = 0;

public GameObject target;

float distancetotarget;

Vector3 angletotarget;



//END AI STUFF//



float zrotate = 0f;

// Use this for initialization

void Start () 

{



}



// Update is called once per frame

void Update () {

//Vector3 pos = transform.position;

	transform.position += transform.forward * (snelheid*throttle);

    Vector3 pos = transform.position;

	if (Fly == true)

	{

	if(throttle<1)

	{

	throttle+=0.005f;

	}

	}

	else

	{

		if(stijging>0)

		{

			

			if(throttle>0)

			{

				throttle-=0.005f;

				

			}

			

		}

		else

		{

			if(pos.y>0)

			{

			if(throttle<1)

			{

			throttle+=0.005f;

			}

			}

			else

		{

			if(throttle>0)

			{

			throttle-=0.005f;

			}

		}

		}

			

		

		

	}

	

	if(pos.y>0)

				{

			if(stijging<90 && stijging>-90)

			{

					stijging-=(0.4f*(1-throttle));

			}

		else

			{

					stijging+=(0.4f*(1-throttle));

			}

		}

	

	pos.y -=  gravity*(1-throttle);	

	

	

	

	if(pos.y<0)

	{

		pos.y+=(-pos.y)*0.1f;

	}

	

	transform.position = pos;		

	

	

	

	

	//AI UPDATE

	

	var targetPoint = target.transform.position;

      var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);

	print(targetRotation);

        //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0); 

	float horizontaal =targetRotation.y + transform.rotation.y;

	float verticaal  = 0;

	

	

	if(target.transform.position.y > transform.position.y + 10 && stijging <50)

	{

		verticaal = 1;

	}

	else

	{

	if(target.transform.position.y < transform.position.y - 10 && stijging >-45)

	{

		verticaal = -1;

	}

		else{verticaal=0;}

	}

	//float horizontaal = transform.rotation.y + targetRotation.y;

	//END AI UPDATE

	stuur += horizontaal;

	

	stijging += verticaal;

	stijging = stijging*0.99f;

	zrotate -= horizontaal;

	zrotate = zrotate*0.99f;

	if(stijging>180)

	{

	stijging=-180;	

	}

	if(stijging<-180)

	{

	stijging=180;	

	}

	

	if(zrotate>90){zrotate=90;}

	if(zrotate<-90){zrotate=-90;}

	

	transform.rotation = Quaternion.Euler(-stijging, stuur, zrotate);

}

}

I found this helper function in Unity’s math library:
[MoveTowardsAngle][1]
http://unity3d.com/support/documentation/ScriptReference/Mathf.MoveTowardsAngle.html

Here’s a thread on wrapping angles:
[wrapping-euler-angles-to-achieve-mouse-look][2]

To save you some time, the easy-to-understand and correct answer is this:

float WrapAngle(float ang) {
    while (ang > 180.0f)
        ang -= 360.0f;
    while (ang <= -180.0f)
        ang += 360.0f;
    return ang;
}

the more mathematically efficient version is this:

float WrapAngle(float ang) {
    int n = (int)(ang / 180.0f);
    if (n & 1)
        n += (n < 0 ? -1 : 1);
    return ang - (float)(n * 180);   
}