Hi, all. I am trying to figure out a way to get my tank to die when flipped, but I can’t figure out where to start. Basically I’m looking for a way to have a script detect when my tank is rotated 180 degrees along the Z or X axis, and kill the tank instantly when this is true.
My problem is that I am not an experienced scripter and I’m not sure what to use to detect that the tank is flipped. All I want is some advice, really. All help would be appreciated.
I have looked through the scripting documents and the scripting tutorial, but I didn’t find anything that seemed to pertain. Although I could have missed it easily
If performance is particularly important you can also check yourTank.transform.up.y is less than some value like -0.8 (when the tank is precisely upside down .y will be -1), at the cost of some code readability.
// prints "close" if the z-axis of this transform looks
// almost towards the target
var target : Transform;
var GameObject;
function Update () {
var targetDir = target.position - transform.position;
var right = transform.TransformDirection(Vector3.right);
var angle = Vector3.Angle(targetDir, right);
if (angle < 180.0)
{
Detonate();
}
}
function Detonate ()
{
Destroy(GameObject);
gameObject.active = false;
}
Here is the code I’ve come up with. But it doesn’t let me pick an object to kill when the tank flips, and it doesn’t kill the children of the object. Basically what happens is it instantly kills the object it is attached to and nothing else. How can I change this code so that is will kill the object and all its children but ONLY when the vehicle is flipped?
var target : Transform;
var betterDestroyMeToo: GameObject;
function Update () {
var targetDir = target.position - transform.position;
var right = transform.TransformDirection(Vector3.right);
var angle = Vector3.Angle(targetDir, right);
if (angle < 180.0)
{
Detonate();
}
}
function Detonate ()
{
Destroy(gameObject);
Destroy(betterDestroyMeToo);
}
Because GameObject is the name of the class, you cant destroy the class!
gameObject instead is a reference to the self game object accessible from any part of an script that derives from MonoBehaviour (and Behaviour… and Component by default).
so calling Destroy (gameObject); is the same as the game object saying “Destroy myself! i want to die ”
Oh, I see. So gameObject refers to the gameObject in question, but GameObject refers to the concept of gameObjects. And you can’t destroy an entire concept. Not the same, but I think I get what you are saying.
Er, I have a question. When I start my game, it sets the first and second rotation values of my tank to over 360. They are at zero when I start the game. Why should it change like that?
It tells me that it is changing the rotation from 0 to 360 when just moving ten feet. This is weird, I never noticed this before.