1
\$\begingroup\$

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;
            }
        }
}
\$\endgroup\$
  • \$\begingroup\$ You could make Waypoint into an object. That way it has internal state like "passed"/"not passed". Would make a lot of things easier in the future (i. e. the goal having a different color). Sorry fot not responding to your original question but I'm on mobile atm \$\endgroup\$ – BlueWizard Jan 29 '18 at 5:36
1
\$\begingroup\$

Hope this helps

public float speed;
public float turnSpeed;

public Transform player;

public Transform[] waypoints;
private int currentlyActive = 0;

void FixedUpdate () 
{
    if(waypoints.Length >= currentlyActive)
    {
        return;
    }
    waypoint = waypoints[currentlyActive];
    player.position = Vector3.MoveTowards(player.position, waypoint.position, speed * Time.deltaTime);
    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);
    if(distance <= 1)
    {
        currentlyActive++;
    }
}

Haven't tested it in unity but it should be the transformation you asked for.

I recommend looking at how others have solved this problem. Often you can see APIs or programming techniques you don't know yet.
Also there are many C# or Unity Books :)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I got it by my own however I just declared public GameObject[] waypoints; and private int currentWp = 0; and then I just replaced the waypoint.position with waypoints[currentWp].transform.position and if the distance is smaller than a number currentWp++; this seemed to work. \$\endgroup\$ – Daniel Jan 29 '18 at 19:37
  • \$\begingroup\$ But thanks your code brought me up to an idea how it could work :) \$\endgroup\$ – Daniel Jan 29 '18 at 19:37
  • \$\begingroup\$ glad to hear :) \$\endgroup\$ – BlueWizard Jan 30 '18 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.