- Home /
Remember the start coordinates?
I just made a roaming script for some animals, etc. The script currently makes them roam endlessy in any direction and graze/pause for every (x) seconds. I have a lot of animals to place in the level, and would like them to wander for only a certain distance in the range of where ever I happen to place them at.
I'm wondering if there is a way to 'record the start transform.position' so that if they wander too far from it, they respawn(walk) back to that point. Before someone shoves a respawn/waypoint tutorial link down my throat, I KNOW ABOUT SETTING SPAWN POINTS! The thing is, there are many different things I have to place that roam, and manually setting each spawn point for each npc seems entirely unnecessary. It would be far more convenient to just put a script on an npc, place it anywhere in the level, and THAT spot is retained as the 'spawn point' automatically in the script when the game plays. DONE.
here is an idea of the script
private var placed = (coordinates of wherever i dumped you)
if ( Vector3.Distance( placed, transform.position ) >= distance ) { transform.position = placed;
And, I have heard of 'insideUnitCircle' but it does not seem to want to work properly. Here is my best attempt
private var placed: Vector2= Random.insideUnitCircle*2 ;
}if ( Vector3.Distance( placed, transform.position ) >= distance ) {
transform.LookAt(placed);
transform.Translate ( speed* Vector3.forward* Time.deltaTime);
animation.CrossFade("walk");
this works in general, but it makes the npc jump to some far off OTHER random place- no where near where I originally placed the animal.
I know the answer is something simple or right under my nose but I can't seem to sniff out a solution. Please- HELP ME! :p Thanks
Here is part of the code
function Start(){ animation.wrapMode = WrapMode.Loop; animation["walk"].layer = -1; animation["idle"].layer = -1; animation.SyncLayer(-1);
InvokeRepeating ("random",5,5);
//These didn't work- the editor keeps complaining that 'placed' in the update function is undefined. This happens if they vars or just plain var placed = gameObject.GetComponent(Transform); var placed = gameObject.transform; placed = transform.position; }
function Update (){
if (transform.position.y <=-1){
transform.position.y =1;
// this is where the problem is. If I could reference 'placed' from the start function, the npc will go back to wherever
// I happen to have dropped it in the level. no extra spawn points or empty game object transforms needed
}if ( Vector3.Distance(placed.position, transform.position ) >= distance ) {
gohome();
Answer by superventure · Dec 10, 2010 at 11:50 AM
No, actually it be would be
private var placed : Vector3;
but thanks anyway. I believe I was simply overthinking the concept.
Answer by Ludovic · Dec 04, 2010 at 02:56 PM
Well, you got the Awake, the Start and the OnEnable methods to do this... See this page to see the differences between these methods Execution Order
BTW, the code you wrote just set the original position somewhere near the origin of the world. You're surely don't want to do that.
var placed:Transform;
function Start() { // Other code
// Set the original transform placed = transform; }
function Update() { // Check out distance here if ( Vector3.Distance(placed.position, transform.position ) >= distance ) { } }
I think you should also check out a tutorial about Unity Javascript like Newbie guide to Unity Javascript or Head First into Unity with JavaScript.
Well, I tried to put "placed" in the start function (var placed = gameObject.transform;) but I keep getting error messages. The distance detecting part of script is in the update function, and the error is that it complains that 'placed' is an unkown identifier. How do I fix this?
try gameObject.transform.position. Also did you declared placed outside of the Start and Update functions ?
I tried that and got an error message: You are not allowed to call this function when declaring a variable...
I included the part of the script in the question for you. Also, about declaring placed outside of start and update- won't that update the start position otherwise? I put 'placed' in start so that it would NOT update the object's starting transform.position...