0
\$\begingroup\$

I come up with this code and I try to get the waypoint + 1 when I reach the waypoint and it adds up till the last waypoint. I get Array is out of Index error. I guess I need and foreach loop of waypoints ?

Code:


public float speed;
public float turnSpeed;

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

void FixedUpdate () 
{

    if(waypoints.Length >= currentWp)
    {
        return;
    }

    player.position = Vector3.MoveTowards(player.position, waypoints[currentWp].transform.position, speed * Time.deltaTime);

    var rotation = Quaternion.LookRotation(waypoints[currentWp].transform.position - player.position);
    player.rotation = Quaternion.Slerp (player.rotation, rotation, turnSpeed * Time.deltaTime);

    float distance = Vector3.Distance(player.position, waypoints[currentWp].position);

    if(distance <= 10)
    {
        currentWp++;
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your test if(waypoints.Length >= currentWp) is backwards.

You want if(currentWp >= waypoints.Length) (ie. "Bail out if we've already reached the last waypoint")

\$\endgroup\$
8
  • \$\begingroup\$ I changed it, still not working.. \$\endgroup\$
    – Daniel
    Commented Jan 29, 2018 at 19:01
  • 2
    \$\begingroup\$ "Not working" is never a sufficient description to debug the issue. Edit your question to include the contents of the waypoints array, and debugging info indicating what line you're encountering the exception on and what the value of currentWp is at that moment. \$\endgroup\$
    – DMGregory
    Commented Jan 29, 2018 at 19:05
  • \$\begingroup\$ I mean with not working that the player isnt going to the waypoints at all, now I dont get an error because of the change what u wrote me thanks for that but it still isnt working like how I wanted it to work, the player should moveTowards the first waypoint and the currentWp what was 0 should add up to 1 to move to the next waypoint when distance is lower than a number, hope you get now what I wanted :) \$\endgroup\$
    – Daniel
    Commented Jan 29, 2018 at 19:09
  • \$\begingroup\$ The code looks fine, so again, you'll have to edit your question to include more debugging information. What is the value of speed? Try inspecting the value of player.position before and after your MoveTowards — if it's not changing, that suggests one route of debugging. If it's changing but you don't see that change in game, that suggests a different route. \$\endgroup\$
    – DMGregory
    Commented Jan 29, 2018 at 19:14
  • \$\begingroup\$ Well thats weird, my players Transform is changing but it isnt moving at all, because I have the script on my player I turned off the Animator component. It is moving now towards each waypoint but without any animations thats not what I want I want to animate while its moving, what could I change that it is still moving towards the waypoints even my Animator component is turned on ? \$\endgroup\$
    – Daniel
    Commented Jan 29, 2018 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .