- Home /
How do I make and object move then respawn at it's start point and move back again in an infinite loop?
Hi I'm new to scripting and was wondering if I could get a little help with getting an object to move forward then when it hits a plane to disappear and respawn at it's starting location and move forward again. I have multiple objects on multiple levels that need this attached. Any help is appreciated aswell as any guides to learning how to script.
There are multiple ways in Unity to move things and detect things. You need to decide how you are going to move it and how it interacts with the rest of the environment before anyone can give you an informed answer.
Well I just want it to move forward on the X axis and when it hits a plane to respawn at its starting point
Answer by BlackWingsCorp · Mar 26, 2013 at 12:50 PM
Man I feel like I'm the dumbest smart ass in the community for not thinking about this (bear with me writing it takes wayyy longer then applying): 1) put your barrel where you want it to be at it starting point 2) move the barrel to where you want it to be in case it didn't hit a plane and check the position's x axis' value in the inspector 3) return the barrel to it's starting position 4) select the barrel and open window --> animation then click on the blank box next to "GameObject" and click create new clip 5) name the animation and save it 6) select position.x and click "Add Curves" then right-click on the brighter grey area under the time (just under the time you feel is best) then add Keyframe PS: 1:00 is 1 second not minute just click, hold and drag the keyframe to edit the time 7)enter the x's new value then close the window 8)click on the animation in the project view and change the wrap mode to loop (inspector view) 9)to make the barrel go back to its original place on contact with any gameobject simply attach to it a new js script and just write these lines (ps: deselect all old scripts you won't need them):
function OnCollisionEnter(col : Collision){
if(col.gameObject){
this.transform.position.x = //original x's value eg: 23.7;
}
}
Now play it: your barrel should move from the starting to the finishing point in a constant loop then thanks to our new script if any gameobject collides with the barrel it will return to it's starting point. Hope this 1 solves the prob. PS: as I said delete the empty gameobject and the scripts I wrote earlier.
Answer by BlackWingsCorp · Mar 23, 2013 at 11:59 AM
ok I found the bug. I kinda "tricked" it to do what I want (the beauty of scripting lol) so you're gonna get an error on the console saying "destroying assets is not permitted to avoid data loss" don't worry cause this "error" tricks the engine on doing exactly what we want it to do. So here's the script you should add to the empty gameobject :
var speed : int = 2000;
var spawnedObject : Rigidbody;
var spawnNumb : int;
function Start(){
spawnNumb = 0;
}
function Update () {
Launch();
}
function Launch(){
//this spawns the object after 2 seconds
yield new WaitForSeconds(2);
if(spawnNumb == 0){
var newspawn : Rigidbody = Instantiate(spawnedObject, transform.position, transform.rotation);
spawnNumb ++;
//moves the object according to the given direction and speed
newspawn.AddForce(Vector3.forward * speed);
yield new WaitForSeconds(2);
Destroy(spawnedObject.gameObject);
spawnNumb --;
}
}
Now add this to the barrel prefab:
function OnCollisionEnter(col : Collision){
if(col.gameObject){
Destroy(this.gameObject);
}
}
Now an instance of the barrel should move forward according the the position/rotation of the empty gameobject will disappear if it hits any gameobject or disappear after 2 seconds in a constant loop. Hope it helps
Answer by kyogretcg · Mar 24, 2013 at 06:07 PM
i keep getting an error saying "unknown identifier: newspawn" how do I fix this. The name of the object I want to move is "Barrel"
It worked well with me. Did you assign the barrel object to the spawnedObject variable in the inspector? And did you attach the script to an empty gameobject?
well thats the problem is when I save the script it give me the error saying it doesn't recognize newspawn and so I can't do anything without that being fixed. I made the empty gameobject and attached my barrel to it but the script is giving me an error. and I'm not seeing the spawnedObject variable in the inspector. Do you mean in the script?
The error is gone but now the barrel stays in place then disappears after two seconds and never comes back. If you'd like I can upload a copy of my game level to mediafire so you can see exactly what I'm trying to accomplish.
thanks man this helped so much I really appreciate the help and one more thing if I may. Do you know any good tutorials or websites where I can learn to script? Thanks again.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094921im_/https://answers.unity.com/themes/thub/images/avi.jpg)