- Home /
Simplifying C# script for guide arrow waypoints
I have an arrow above Player's head to guide him to the exit of level. There are 13 waypoints to avoid arrow looking in only one direction. Also arrow has a script to collide with waypoints and switch to next waypoint. I'm wondering if there is some other way to write these functions, because this is veeeery long... Maybe you have some ideas to make it more simple.
Thanks!
using UnityEngine; using System.Collections;
public class arrowScript : MonoBehaviour {
public bool wp2 = true; public bool wp3 = false; public bool wp4 = false; public bool wp5 = false; public bool wp6 = false; public bool wp7 = false; public bool wp8 = false; public bool wp9 = false; public bool wp10 = false; public bool wp11 = false; public bool wp12 = false; public bool wp13 = false; public Transform wayp2; public Transform wayp3; public Transform wayp4; public Transform wayp5; public Transform wayp6; public Transform wayp7; public Transform wayp8; public Transform wayp9; public Transform wayp10; public Transform wayp11; public Transform wayp12; public Transform wayp13;
void OnTriggerEnter (Collider col) { if (col.tag == "wp1") { wp2 = true; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp2") { wp2 = false; wp3 = true; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp3") { wp2 = false; wp3 = false; wp4 = true; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp4") { wp2 = false; wp3 = false; wp4 = false; wp5 = true; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp5") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = true; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp6") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = true; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp7") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = true; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp8") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = true; wp10 = false; wp11 = false; wp12 = false; wp13 = false; } if (col.tag == "wp9") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = true; }
if (col.tag == "wp10") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = true; wp12 = false; wp13 = false; } if (col.tag == "wp11") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = true; wp13 = false; } if (col.tag == "wp12") { wp2 = false; wp3 = false; wp4 = false; wp5 = false; wp6 = false; wp7 = false; wp8 = false; wp9 = false; wp10 = false; wp11 = false; wp12 = false; wp13 = true; } }
void Update (){ if(wp2){ transform.LookAt(wayp2); } if(wp3){ transform.LookAt(wayp3); } if(wp4){ transform.LookAt(wayp4); } if(wp5){ transform.LookAt(wayp5); } if(wp6){ transform.LookAt(wayp6); } if(wp7){ transform.LookAt(wayp7); } if(wp8){ transform.LookAt(wayp8); } if(wp9){ transform.LookAt(wayp9); } if(wp10){ transform.LookAt(wayp10); } if(wp11){ transform.LookAt(wayp11); } if(wp12){ transform.LookAt(wayp12); } if(wp13){ transform.LookAt(wayp13); } }
}
I guess you can do this with a hashtable of some kind, but im kinda horrible at making those :/
Answer by KeithK · Apr 05, 2011 at 10:23 AM
Have your Waypoint object store it's nextWaypoint and call a function on your arrow to set it's current waypoint.
EDIT: Changed as per Bunny's comment.
So in your script on your Waypoint object:
public Transform nextWaypoint = null; // Can drag the next waypoint from the scene editor.
void OnTriggerEnter(Collider TheCollider) { if (TheCollider.gameObject.tag == "arrow") // May want to make sure it's the arrow. { TheCollider.gameObject.GetComponent<ArrowScript>().SetCurrentWaypoint(nextWayPoint); } }
Then in your ArrowScript of your Arrow:
private Transform _currentWaypoint = null;
public void SetCurrentWaypoint(Transform Waypoint) { _currentWaypoint = Waypoint; }
void Update() { if (_currentWaypoint != null) { transform.LookAt(_currentWaypoint); }
}
Replace all occurrences of "ArrowScript" in my example here with whatever the name of the script attached to your Arrow is.
In your case the arrow will be rotated towards the target, but it won't update when you move around. You should call the function SetCurrentWaypoint or something like that and save the current Transform inside the Arrow script. The lookat should be in Update() or at least called frequently.
Answer by Owen-Reynolds · Apr 05, 2011 at 04:35 PM
Read a little about arrays. All languages have them, and they all work about the same. The general idea is 13 vars turn into one var with 13 parts. 13 ifs turn into one "lookup by number." A general (Unity-style) setup is:
// In the Inspector, click the arrow and enter the size. Slots will appear: public Transform[] Wayp2;
// if you have 13 slots, they are numbered 0 to 12, so 0 is the 1st waypoint: int curWp = 0; // NOTE: curWp completely replaces that list (wp#) of 13 T/F
// How to get current target: Vector3 walkHere = Wayp[curWp].position; // Ex: if curWp is 2, this looks up Wayp[2], which is the 3rd spot in the list // NOTE: this completely replaces that 2nd set of 13 ifs
Your answer
Follow this Question
Related Questions
Waypoint System 1 Answer
Is this an [optimal, adequate, ftarded] way to make a snake follow its head? 2 Answers
assign waypoints on a prefab 1 Answer
Waypoint tree structure 2 Answers