- Home /
Question by
woutervangerven · Dec 21, 2016 at 07:30 AM ·
argumentoutofrangeexception
Argument out of range Exception & Waypoint problem
Hey guys,
I got an problem the problem sounds like this... I have a system that sets up waypoint for an airplane to travel to. The first problem is that the waypoints aren't set and the second problem is that it gives an argument out of range error if I try to start it. I have an boolean thats called started that I have to enable to let it start.
(The Argument out of range problem could be because I can't set waypoints, and i'm dutch so don't mind my english xD)
Update: (The fault if in line 89. Thats the line that begins with: Vector 2 dir = ....)
Here is my code: using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Waypoint : MonoBehaviour {
List<GameObject> waypoints = new List<GameObject>();
public GameObject waypoint;
public float time = 0;
public int current = 0;
public int PreCurrent = 0;
Vector3 startpos;
Vector3 endpos;
Quaternion startrot;
[Range(1,10)]
public float speed = 1;
[Range(1, 10)]
public float rotspeed = 1;
public int lastplaced = -1;
public bool started = false;
public bool rotating = true;
void Start () {
startpos = transform.position;
startrot = transform.rotation;
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
Vector3 s = Camera.main.ScreenToWorldPoint(Input.mousePosition);
s.z = 0;
s.x = Mathf.Round(s.x);
s.y = Mathf.Round(s.y);
bool straight = false;
Vector2 go;
if (lastplaced == -1)
{
go = transform.position;
}
else
{
go = waypoints[lastplaced].transform.position;
}
if ((int)go.x == (int)s.x || (int)go.y == (int)s.y)
{
straight = true;
}
if (straight)
{
//waypoints.Add((GameObject)GameObject.Instantiate(waypoint, s, new Quaternion()));
//lastplaced++;
}
else
{
Debug.LogError("You cannot go sideways!");
}
}
if(started && !rotating)
{
transform.position = Vector3.Lerp(startpos, waypoints[current].transform.position, (time * speed / Vector2.Distance(startpos,waypoints[current].transform.position)));
endpos = Vector3.Lerp(endpos, waypoints[current].transform.position, (time * speed / Vector2.Distance(endpos, waypoints[current].transform.position)));
if(time * speed / Vector2.Distance(startpos,waypoints[current].transform.position) >= 1)
{
current++;
if (waypoints.Count == current)
{
started = false;
}
time = 0;
startpos = transform.position;
startrot = transform.rotation;
rotating = true;
}
time += 0.01f;
}
else if (rotating && started)
{
Vector2 dir = waypoints[current].transform.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
Quaternion rot = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Lerp(startrot, rot, time * rotspeed);
if (time * rotspeed >= 1)
{
rotating = false;
time = 0;
}
else if(rot == startrot)
{
time = 0;
rotating = false;
}
time += 0.01f;
}
}
}
Comment