- Home /
Adding Waypoints using a List
Dear Coders I'm trying to add Waypoints to a dynamic list. So far, I've added GameObjectWithTag "Waypoint" to the list - but the code below only allows for a waypoint to be constantly added to a list.
I'm trying to randomly instantiate a waypoint as the game progresses and only add the waypoint once to the list but I really don't know where to start with this.
Code is below - any help would be gratefully appreciated!
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq;
public class WaypointManager : MonoBehaviour {
public Transform wayPointManager;
private GameObject wayPoint;
public List<GameObject> wayPoints = new List<GameObject>();
void Start ()
{
wayPoint = GameObject.FindGameObjectWithTag("WayPoint");
wayPoints.Add (wayPoint);
}
void Update (){
if (wayPoints == null || wayPoints.Count == 0)
{
return;
}
wayPoints.Add (wayPoint);
wayPoint.transform.parent = wayPointManager;
}
}
Answer by karma0413 · Dec 16, 2017 at 04:42 PM
Does waypoint have additional information or scripts on the gameObject? Why use a gameObject to define a wayPoint instead of using like a Vector3 ( an x,y, z position ) ? I am just curious....
THIS, if Waypoints are empty... will actually INSTANTIATE a new gameObject in the scene and then add it to your list. Is that what you needed? Are you needing GameObject waypoint to be a physical gameObject in the scene?
public Transform wayPoint$$anonymous$$anager;
public GameObject wayPoint;
public List<GameObject> wayPoints = new List<GameObject>();
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
if (wayPoints == null || wayPoints.Count == 0)
{
wayPoints.Add(GenerateARandomWayPoint());
}
}
// --------------------------------------------Sub Routines--------------------------------------------
// --------------------------------------------Sub Routines--------------------------------------------
// --------------------------------------------Sub Routines--------------------------------------------
// --------------------------------------------Sub Routines--------------------------------------------
// --------------------------------------------Sub Routines--------------------------------------------
// --------------------------------------------Sub Routines--------------------------------------------
public GameObject GenerateARandomWayPoint() // this will return back a gameObject of our choosing
{
bool setRelativeToWorld = true; // Set to TRUE if you want the object to spawn in the world coordinate system or set to FALSE if you wany the object to spawn in position relative to parent.
GameObject clone = Instantiate(wayPoint, wayPoint$$anonymous$$anager, setRelativeToWorld) as GameObject;
//clone.transform.position = ;
//clone.transform.localPosition = ;
return clone;
}
You can Instantiate gameObjects using a variety of ways: (Taken from documentation)
Instantiate(Object original);
Instantiate(Object original, Transform parent);
Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
Instantiate(Object original, Vector3 position, Quaternion rotation);
Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
Thankyou very much for your help - this has worked perfectly. However, I'm trying to instantate the wayPoint at a random position and for some reason Random.Range is not working.
Would you be able to point in the right direction? Thankyou!
public class Waypoint$$anonymous$$anager : $$anonymous$$onoBehaviour {
public Transform wayPoint$$anonymous$$anager;
public GameObject wayPoint;
public List<GameObject> wayPoints = new List<GameObject>();
public float timer;
void Start ()
{
wayPoint = GameObject.FindGameObjectWithTag("WayPoint");
wayPoints.Add (wayPoint);
}
void Update (){
timer += Time.deltaTime;
if (timer > 10.0f) {
GenerateARandomWayPoint ();
timer = 0f;
}
if (wayPoints == null || wayPoints.Count == 0)
{
wayPoints.Add (GenerateARandomWayPoint());
}
}
public GameObject GenerateARandomWayPoint(){
bool setRelativeToWorld = true;
GameObject clone = Instantiate (wayPoint,wayPoint$$anonymous$$anager,setRelativeToWorld) as GameObject;
clone.transform.position = new Vector3 (10,1,10);
return clone;
}
}
If my reply has helped you in some way, please give it a +1 or accept it at the correct answer. I am trying to build my reputation.
Let me pull my visual studio to make sure I give you the correct code... Okay, well first of all you could go about this in many ways, but I think an important consideration we must think about is
A) When we spawn an object, do we first want to make sure it does not collide with something? IE: Is there a wall right there? or another spawn point?
B) Who controls the spawning and how do we decide to do that?
For me in my program, it was a monster spawner so I had wanted to spawn in a radius and I also wanted to make sure another collision wasn't already occuring (IE: through the environment or because there's another monster already right there but has not yet moved out of the way)
There is also Y (height consideration) you need to think about when spawning items.
I also recognized that you set it's transform as a parents ( in the previous code ), but then you set it's WORLD position by utilizing the code: clone.transform.position = someVector3 , I just want to make sure you don't want to anchor it around the main transform parent in which case you would utilize the code: clone.transform.localPosition = someVector3. Also if it does not need a parent you can change the Instantiate code so your not settings its Transform as parent.
clone.transform.position = RandomPosition()
//later down in the code you can return that Vector3 with a function like so...
public Vector3 RandomPosition()
{
Vector3 thisVector3 = new Vector3();
thisVector3.x = UnityEngine.Random.Range($$anonymous$$FloatValue, maxFloatValue);
thisVector3.y = UnityEngine.Random.Range($$anonymous$$Floatvalue, maxFlaotValue);
thisVector3.z = UnityEngine.Random.Range($$anonymous$$floatValue, maxFloatValue);
return thisVector3;
}
Of course... You could simply set the range with code like this:
thisVector3.x = UnityEngine.Random.Range(0f, 10f);
thisVector3.y = UnityEngine.Random.Range(0f, 0f);
thisVector3.z = UnityEngine.Random.Range(0f, 10f);
Answer by ohkayem · Dec 16, 2017 at 04:48 PM
@karma0413 I hadn't thought of it like that although it could work. Once instantiated the waypoints need to remain in game as the enemies will continue to patrol randomly along them and so that's why I used gameobjects.
How would this work with vectors - wouldn't the problem still be the same with the way the script is?
Thanks!
I replied with a solution using gameObjects and instantiation.... Please see above...