- Home /
All gameObjects with specific tag disable z axis as to not fall forward or backward?
Hello. I need to make it so multiple game objects do not fall forward or backwards, as to kind of act like a 2d game. An alternative to the tag would be to have the script on each gameObject. This method would actually be preferred. Thank you! -Keavon
Answer by Peter G · Jan 05, 2011 at 12:44 AM
You could manually go through and add components to all the objects that you want. If you wanted a script to do it at runtime, you could do something like this which will add them automatically for you:
//SceneManager.js //This goes somewhere in your scene. function Awake () { var specialObjs : GameObject[] = GameObject.FindGameObjectsWithTag("SomeTag");
for(var obj : GameObject in specialObjs) {
var comp : KeepOnZ = obj.AddComponent(KeepOnZ);
comp.Init();
//You could probably let Start() call itself in your other script.
}
}
//KeepOnZ.js var zDepth : float;
function Init () { zDepth = transform.position.z; //Initialize your script. }
function Update () { transform.position.z = zDepth; //if you are going to be locked on the z-axis, then you can do: //transform.position.z = 0.0;
//Or you could add some joints to lock its position as well.
//Probably a Configurable joint.
transform.eulerAngles.z = 0;
//Don't let it rock side to side.
}
Wow! Thank you so much! I just used the lower script and it worked great (I removed the comments). I didn't actually need to add it to every one at the beginning, as I just added it to the prefab and it worked like a charm! Best answer. Thank you! $$anonymous$$y game is really progressing!
Answer by Justin Warner · Jan 05, 2011 at 12:36 AM
Try (Didn't test):
var yAxis;
function Awake () { yAxis = transform.position.y; } function Update () { transform.position.y = yAxis; }
That MIGHT work.
Thank you. I did ask for a z axis, but that isn't hard to change. I actually used the script from Peter G, though I think yours would have worked too. You might have been missing the "transform.eulerAngles.z = 0;" thing, but I don't now what that does. I shall vote up your answer for your effort (does that increase your reputation score?).
@$$anonymous$$eavon, the eulerAngles thing just makes sure that your objects won't swing off the xy plane. $$anonymous$$ost of the time your won't need it, it just makes sure in those special cases.