I want to simplify my code. I had the idea of making an Array of waypoints[] and and int i to count up each time I arrive a new point of waypoint but it should be the same way my code is but instead of waypoint1.position... waypoint3.position I thought it should be like waypoint[i].position and it counts up everytime I reach another waypoint this would simplify a lot and would make the code much shorter. The code is for moving an Object from A to B from B to C and so on..
I commented some lines how it would look like but ignore the //MOVE //ROTATE comments
Code:
public float speed;
public float turnSpeed;
//Starting with these somehow
//public Transform[] waypoints;
//private int i = 0;
public Transform player;
public Transform waypoint;
public Transform waypoint1;
public Transform waypoint2;
public Transform waypoint3;
public bool b0 = true;
public bool b1 = false;
public bool b2 = false;
public bool b3 = false;
public bool b4 = false;
void FixedUpdate ()
{
if(b0)
{
//MOVE
player.position = Vector3.MoveTowards(player.position, waypoint.position, speed * Time.deltaTime);
//ROTATE
var rotation = Quaternion.LookRotation(waypoint.position - player.position);
player.rotation = Quaternion.Slerp(player.rotation, rotation, turnSpeed * Time.deltaTime);
float distance = Vector3.Distance(player.position, waypoint.position);
//Debug.Log (Mathf.Round(distance));
if(distance <= 1)
{
// i++;
b1 = true;
b0 = false;
}
}
if(b1)
{
//MOVE
player.position = Vector3.MoveTowards(player.position, waypoint1.position, speed * Time.deltaTime);
//ROTATE
var rotation1 = Quaternion.LookRotation(waypoint1.position - player.position);
player.rotation = Quaternion.Slerp(player.rotation, rotation1, turnSpeed * Time.deltaTime);
float distance1 = Vector3.Distance(player.position, waypoint1.position);
//Debug.Log (Mathf.Round(distance1));
if(distance1 <= 1)
{
b1 = false;
b2 = true;
}
}
if(b2)
{
//MOVE
player.position = Vector3.MoveTowards(player.position, waypoint2.position, speed * Time.deltaTime);
//ROTATE
var rotation2 = Quaternion.LookRotation(waypoint2.position - player.position);
player.rotation = Quaternion.Slerp(player.rotation, rotation2, turnSpeed * Time.deltaTime);
float distance2 = Vector3.Distance(player.position, waypoint2.position);
//Debug.Log (Mathf.Round(distance1));
if(distance2 <= 1)
{
b2 = false;
b3 = true;
}
}
if(b3)
{
//MOVE
player.position = Vector3.MoveTowards(player.position, waypoint3.position, speed * Time.deltaTime);
//ROTATE
var rotation3 = Quaternion.LookRotation(waypoint3.position - player.position);
player.rotation = Quaternion.Slerp(player.rotation, rotation3, turnSpeed * Time.deltaTime);
float distance3 = Vector3.Distance(player.position, waypoint3.position);
//Debug.Log (Mathf.Round(distance1));
if(distance3 <= 1)
{
b3 = false;
b4 = true;
}
}
}