- Home /
switching turns between two game objects.
Hi Everyone
Can anyone help me with this problem please. I've got this scenario where a game object move forwards and after 26 seconds it respawns back to its original position. Now directly opposite this first game object is a second game object that does the same thing as the first game object. The only problem I have is I want them to take turns moving forward and respawning. So the first one moves forward then respawns & stops moving. The second one then moves forward, respawns & stops moving allowing the first one to move forward. You get the picture the only other thing that I would like to add would be that is would have to be continuous cycle. Here's my code so far:
var Sec2Respawn : float = 26.0;
function Update(){
transform.Translate(0, 0, -1 * Time.deltaTime);
if(Sec2Respawn > 0){
Sec2Respawn -= Time.deltaTime;
}
if(Sec2Respawn <= 0){
Sec2Respawn = 26.0;
var parentMill = GameObject.Find("MovementWindMill");
parentMill.transform.position = Vector3(2.508217, 6.518723, 25.69707);
}
}
Help much appreciated. Thanks
When posting code, please format it with the 1010101 button and use proper indentation.
You need an external master code to handle the switching.
Answer by Eli-Davis · Jul 08, 2011 at 04:09 AM
Heres a method you could consider about using. Feel free modifying it the way you want. comment if you have any questions or problems var respawnTime : float = 26.0;
var firstObjectEnabled = true;
function Update(){
var gameObectOne = GameObject.Find("Your game object");
var gameObectTwo = GameObject.Find("Your other game object");
if(firstObjectEnabled == true){
if(respawnTime > 0){
gameObectOne.transform.Translate(0, 0, -1 * Time.deltaTime);
respawnTime -= Time.deltaTime;
}
if(respawnTime <= 0){
respawnTime = 26.0;
//reset to position you want
firstObjectEnabled = false;
}
} else {
if(respawnTime > 0){
gameObectTwo.transform.Translate(0, 0, -1 * Time.deltaTime);
respawnTime -= Time.deltaTime;
}
if(respawnTime <= 0){
respawnTime = 26.0;
//reset to position you want
firstObjectEnabled = true;
}
}
}
Your answer
Follow this Question
Related Questions
switch object when collided with? 2 Answers
Help needed in changing weapons 2 Answers
how well does unity work for turn-based game time-type? 3 Answers
RPG Action Progress Bar always same speed 1 Answer
Weapon pick up and switching script 2 Answers