Hi, simple question.
How to move an object to the sky infinitely ?
I have this script, but he doesn’t work…
I’m only able to play the audio after 25 seconds, but he stay at the same place.
var sound : AudioClip;
public var speed : float = .1f;
function Start(){
Teleport();
}
function Teleport(){
yield WaitForSeconds(25);
AudioSource.PlayClipAtPoint(sound, transform.position );
transform.position.y += speed * Time.deltaTime;
}
Any Ideas ?
Use a loop in your Teleport function:
function Teleport(){
yield WaitForSeconds(25);
AudioSource.PlayClipAtPoint(sound, transform.position );
while (true) {
transform.position.y += speed * Time.deltaTime;
yield;
}
}
This avoids having to use global variables and prevents potential spaghetti code mess of if/thens in Update if you have more complex behavior.
Pretty much as Lo0NuhtiK said, there’s no real need to have your movement inside the teleport
function if you want to move it constantly:
var sound : AudioClip;
public var speed : float = .1f;
public var isMoving : boolean = true;
function Start(){
Teleport();
}
function Update(){
if(isMoving){
transform.position.y += speed * Time.deltaTime;
}
}
function Teleport(){
yield WaitForSeconds(25);
AudioSource.PlayClipAtPoint(sound, transform.position);
isMoving = false;
}
I’m guessing you want the object to stop after 25 seconds btw, your question wasn’t entirely clear. If you don’t, then just take out the isMoving = false;
line, or remove the variable entirely