- Home /
Renaming issue for a waypoint system
Hello- I've been working on a waypoint system and have run into some trouble. The script should move unit to waypoint, which is then destroyed and waypoint2 is turned into waypoint, and the process is then repeated. However, while it reaches waypoint fine, waypoint2 will not turn into waypoint- it simply stops moving at waypoint. Any ideas as to why this is happening? Many thanks for any light you can shed on this!
var unit: GameObject;
var waypoint: GameObject;
var waypoint2: GameObject;
var duration: float;
var collision: Collision;
private var starttime: float;
function start(){
//sets start time for comparison on motion
starttime = Time.time;
}
function Update () {
//detects for the presence of a waypoint
if (GameObject.Find("waypoint") != null){
//if a waypoint is present, move it to its location over the given duration
var startpos: Vector3 = unit.transform.position;
var endpos: Vector3 = waypoint.transform.position;
transform.position = Vector3.Lerp(startpos, endpos, (Time.time - starttime)/duration);
}
}
function OnCollisionEnter(collision : Collision)
{
//when the unit collides with the waypoint, destroy it
if(collision.collider.gameObject.name == "waypoint")
{
Destroy(waypoint.gameObject);
waypoint2.name = "waypoint";
waypoint = waypoint2;
starttime = Time.time;
}
}
Oh, and another thing (although I'm probably not quite thinking clearly right now, it seems like it should be pretty simple, although I for some reason can't think it through...)- does anyone have any ideas as to how to make this unit-specific? Meaning that when a unit is 'selected' the waypoints (placed in gameplay) are applied to that unit in particular, since I'm going to be dealing with a bunch of them... Any ideas? Thanks!
I'd highly recommend doing it differently than this as this seems a bit unreliable, and a bit overly difficult way of doing it if you ever want 100 waypoints or something. I'd love to help you out making a more simplistic and reliable way of doing it if you want.
As for this code, I don't know whats wrong because I don't know how your scene looks like, also do you get any errors?
The current code doesn't give any errors.
I'd appreciate any help on simplifying this down- what would you suggest? Thanks!
I'd just make a list of vector3, and then make the character move to the first one and then when he is there you just move to the second one, etc. This way you can also make him stop at different waypoints if you'd want.
You don't need to introduce the physics engine into this with collision detection and what not, I'd be happy to send you a unity package example if you'd like that.
You can reach me on Skype at; implementation92, if you want me to go into more details, I'm always happy to help out! :)
Thanks! I stopped using collision detection and replaced it with distance detection. Thanks for your help!
Your answer
Follow this Question
Related Questions
Is it possible to rename tabs in editor? 2 Answers
Force Rename of GameObject in Hierarchy 1 Answer
Waypoint Error. 1 Answer