hi.
i have been trying to write a code for a trigger that i’m not able to make work.
i have a missile launcher, with a sfere collider set as trigger.
whenever the Player enters the sphere it should launch a missile.
if other objects enter the sphere (eg rockets shot by the player), the launcher should no nothing.
this is solved if an If statement checking the collider tag.
void OnTriggerEnter(Collider enemy)
{
if (enemy.gameObject.tag == "Player")
{
Debug.Log ("in range");
InvokeRepeating ("launcher", 3f, timer);
}
}
here is the point.
the if statement won’t work…
it does not work with compare tag either.
the same piece of code and tag is used to check if the player lands on certain zones, using the normal collision and works just fine, and the Player tag is there by default in unity…
i don’t understand.
cpt_cikho:
i don’t understand.
And in return I don’t understand this:
Why are you asking (perhaps expecting?) if your enemy is tagged “Player” ??
That doesn’t seem right.
Sounds like you wrote a bug… and that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
enemy is just a name assigned to the object colliding with the collider…
could be “other”, it should be the same, isn’t it?
Find out by debugging! Print the tag. Print the name! Print ALL the things!!!
Read the docs for collisions / triggers carefully: there are several VERY specific requirements or it simply will not work.
i will, thanks!
what i donpt understand is why the same IF statement works with collision and not with triggers…
void OnCollisionEnter (Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Debug.Log ("landing confirmed");
GetComponent<AudioSource> ().Play ();
score_M_ldg.score = score_M_ldg.score + LND_score ;
}
this works just fine, the if statement is basically the same…
oh, obviosuly if i remove the IF statment, the triggers works just fine and missiles are fired…
Well we know that comparing strings would work always the same, so look elsewhere.
I’m guessing nothing is calling the method where you don’t see it succeed.
Or something else is happening.
As I said, debug, debug, debug. Everything else is just guessing.
1 Like
i think i somehow got it sorted.
the Player tag was assigned to the object containing the rigidbody, which didn’t have a collider… added box collider to it and it worked…
just noticed the the trigger requires a second collider entering it
1 Like
THE POWER OF DEBUGGING!
As you can see, studying all the code in the world would not have revealed this.
Glad to hear you’re sorted.
2 Likes