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.
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
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”
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):
-
put your barrel where you want it to be at it starting point
-
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
-
return the barrel to it’s starting position
-
select the barrel and open window → animation then click on the blank box next to “GameObject” and click create new clip
-
name the animation and save it
-
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.