Autopilot

Hey, I am working on something where I have a flying craft in a circular track that is bounded by walls left and right all the way around the track. I want to have an Autopilot mode where it will fly around the track. I thought of doing this with raycasts and setting limitations.

Here is the code so far:

private var distanceToGround;
private var distanceUp;
private var distanceLeft;
private var distanceRight;
private var distanceForward;
private var forwardNormal;
function Update() 
{
	// always mvoe forward 
	this.rigidbody.AddRelativeForce(Vector3(0, 0, 200));
	
	// down
	var hit : RaycastHit;
    if (Physics.Raycast (this.transform.position, -Vector3.up, hit, 100.0)) {
        distanceToGround = hit.distance;
        
    }
    if (distanceToGround < 5.0)
    {
    	this.transform.position.y = Mathf.Lerp(this.transform.position.y, this.transform.position.y + (4.0 - distanceToGround), Time.time);
	}
	
	// up
    if (Physics.Raycast (this.transform.position, -Vector3.up, hit, 100.0)) {
        distanceUp = hit.distance;
        
    }
    if (distanceUp > 20.0)
    {
    	this.transform.position.y = Mathf.Lerp(this.transform.position.y, this.transform.position.y + (12.0 - distanceToGround), Time.time);
	}
     
    // forward for turning   
    if (Physics.Raycast (this.transform.position, -Vector3.up, hit, 100.0)) {
    	distanceForward = hit.distance;
        forwardNormal = hit.normal;
    }
    Debug.Log(distanceForward);
    if (distanceForward <= 25)
    {
    	var checkVector : Vector3 = transform.InverseTransformDirection(forwardNormal); 
    	
    	if (checkVector.x > 0) 
    	{ 
       	  transform.Rotate(0, Time.deltaTime * 30, 0);     
    	} 
    	else
    	{ 
    	    transform.Rotate(0, -Time.deltaTime * 30, 0); 
    	}   
	}
}

Right now its movement is kinda working. I always tell the craft to go forward on its local axis. When it gets to the wall whether it be left or right i need it to turn. I dont know if the code is right. Its based off of what Marty did in another thread. Sometimes the craft just goes in circles. Other times it works, and sometimes the craft skips hops jumps and then goes bonkers. Any suggestions is appreciated!

Couldn’t you manually add some points in a LinkedList where you want the plane go, and then just lerp the current position with the next one?

Edit: Of course, the last node of the LinkedList would be connected to the first node…

The problem I had with way-points was that the movement was all linear.

Here is the update code.

private var distanceToGround;
private var distanceUp;
private var distanceLeft;
private var distanceRight;
private var distanceForward;
private var forwardNormal;
public var playerGlider : GameObject;

function Update() 
{
	// check if the timer went 3.. 2.. 1.. Go!
	if (Countdown.ableToGo == true)
	{
		// always mvoe forward 
		this.rigidbody.AddRelativeForce(Vector3(0, 0, 700));
	
		// down
		var hit : RaycastHit;
		     
    	// forward for turning   
    	if (Physics.Raycast (this.transform.position, transform.TransformDirection (Vector3.forward), hit, 100.0)) {
    		distanceForward = hit.distance;
    	    forwardNormal = hit.normal;
    	}
    	//Debug.Log(distanceForward);
   		if (distanceForward <= 30)
    	{
    		var checkVector : Vector3 = transform.InverseTransformDirection(forwardNormal); 
    		
    		if (checkVector.x > 0) 
    		{ 
     	  	    transform.Rotate(0, Time.deltaTime * checkVector.x * 15, 0);     
    		} 
    		else
    		{ 
    		    transform.Rotate(0, -Time.deltaTime * checkVector.x * 15, 0); 
    		}   
    	}
    	
    	if (Physics.Raycast (this.transform.position, -Vector3.up, hit, 100.0)) {
    	    distanceToGround = hit.distance;
        
    	}
    	if (distanceToGround < 12.0)
    	{
    		this.transform.position.y = Mathf.Lerp(this.transform.position.y, this.transform.position.y + 1, Time.deltaTime);
		}
    	else
    	{
    		this.transform.position.y = Mathf.Lerp(this.transform.position.y, playerGlider.transform.position.y, Time.time);
		}
	}
}

I set the height of the Auto Pilot plane to that of the plane you control unless the auto pilot plane is below a certain distance to the ground then I move the plane up so it doesn’t crash into the ground.

I do this

if (checkVector.x > 0) 
{ 
     transform.Rotate(0, Time.deltaTime * checkVector.x * 15, 0);     
}

To turn the ship left or right. the check-Vector is either positive or negative if its positive i turn right, negative i turn left. 15 is just a multiplier.

It works fairly well but I need suggestions to improve on it. I dont know if my turning code is right.