yesterday I was trying to instantiate enemies according to the position of a spaceship on the z axis.
I had another idea, to set invisible walls along the spaceship’s path to instantiate the enemies OnTriggerEnter.
I created a wall with a box collider, removed the mesh renderer as to be invisible and attached the followin script:
var enemy1 : Transform;
function OnTriggerEnter (other : Collider) {
if (collider.gameObject.name == "Spaceship") {
Instantiate (enemy1, transform.position, transform.rotation);
}
}
the first problem is that it doesn’t work!!! It does not instantiate enemies (doesn’t have a spelling mistake either, no error). Even though it seems that everything is ok, it doesn’t work.
the second problem is that the missiles have a script to be destroyed OnTrigger (since I want to destroy missiles when they hit an enemy) and because the invisible wall has a trigger, they are destroyed when they hit the wall. How can I make the missiles ignore the trigger of the wall but not ignore the trigger of the enemies???
For triggers to work, one of the objects needs a Rigidbody attached. It can be kinematic so it doesn’t actually do anything, but it needs to be there so the physics engine pays attention to it when it moves, and registers the trigger entry.
You’ll have to do the same thing you’re doing with the enemy spawning triggers: check what object caused the trigger event, and then decide whether to explode or not.
Muriac, both the spaceship and the wall have a rigibody and a box collider. I have to say also that the spaceship is a parent of another gameobject, which also has other 3 childs, maybe is something wrong with that?
I was thinking that maybe the script is not the right one. you think it’s correct? (I don’t get any errors though)
When something enters the trigger, the physics engine calls OnTriggerEnter(), and it passes a reference to the other collider as “other”. You can call it anything you want, but it’s defined at the beginning of your function in the parentheses right after OnTriggerEnter.
Before, you were checking if your own collider’s gameObject had the name “Spaceship”, which of course it never did.
I find things quite hard to explain, I tend to learn such things by osmosis, with repeated examples many times over. Unfortunately.
Really? He had capital C in the brackets and lower case c in the function, I thought they were two different things…But I really dont know so please let us know?
Thanks, glad it worked Redghost. Incidentally, you could also do this:
var NameOfControlSystem : String;
var enemy1spawnpoint : GameObject;
var enemy1 : Transform;
function OnTriggerEnter (other : Collider) {
if (other.gameObject.name == NameOfControlSystem) {
Instantiate (enemy1, enemy1spawnpoint.transform.position, transform.rotation);
}
}
Youd make the control System Spaceship in your context. You make an empty game object and use its position to define the spawnpoint.
AC
“Collider” refers to the Collider class, and in the context of the function parameter (being after the “:”) indicates that the variable “other” is a Collider.
“collider” refers to the field named collider in the current script. This field is inherited from Component (which all Components, including MonoBehaviours such as most scripts you write). It refers to the collider (if any) that is attached to the GameObject to which the Component is attached. So if you reference “collider” from a script, you are referring to the Collider attached to the same GameObject as the script.
In the script redghost originally posted, he was checking collider.gameObject.name, which is the name of the GameObject of the Collider of the GameObject of the current script (follow?). This is the same as referring directly to “name” (another member inherited from Component), or the name of the GameObject to which the current script is attached. This wouldn’t tell you anything about the other object. In order to find that, we reference “other.name” (“other” being the Collider passed into the function).
Guys, you are so helpful! Thank you very much!! Targos, the idea of using the empty gameobject as a reference for the position of the spawning enemies is absolutely great (and clever!!).
the only thing left is to destroy the missiles only when they hit the enemies and not the walls and to do that I will need to use a tag in order to refer to the enemies. So a script like that i think is what is needed:
On TriggerEnter ( other : Collider) {
if (other.gameObject.tag == "enemy") {
Destroy(gameObject);
}
}
Is it correct to use “tag” like this? (instead of name, I mean)
Yes!!! It seems that it works great. In this way, using tags, I could have also 2 or 3 different kind of weapons on the ship that each one of them can destroy different kind of enemies. For example, green missiles can destroy only green enemies, red missiles can affect only red enemies (In a way like the videogame “Ikaruga” with the black and white alternation)