- Home /
Problem triggering using colliders
Hi, I just started Unity a couple days ago. I am primarily an audio designer and am completely new to coding, so it's been a bit of a challenge at times. I've been following tutorials to put together a 2D platformer.
I've been using this Sebastian Lague Youtube series to do the mechanics https://www.youtube.com/playlist?list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz
I've also used this Unity audio tutorial to get my music set up in mixers and snapshots, since I want the music to change as you progress https://unity3d.com/learn/tutorials/topics/audio/adding-music-your-game
After the platforming mechanics were set-up, and after setting up all the mixers, I've had problems doing the two following things, which together make me think there's a problem with how things are colliding:
1) Making a collectable item/coin that would be destroyed when colliding with the player. The player pushes around the coin instead of destroying it. I added the print line to see if this script was running at all, and the text did not show up in the console when pushing around the coin.
void OnCollisionEnter2D (Collision2D col) { if (col.gameObject.tag == "Player") { Destroy (gameObject); print ("nomnom"); } }
2) Using collision with a trigger zone to trigger transitions between mixers. The audio does not change when the player enters the designated zone. I've followed all the steps in the Unity tutorial here, changing 3D objects for 2D ones, and adding "2D" (i.e. Collider2D, etc.) to some of the code where necessary.
void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag ("TargetZone1")) { bg2a.TransitionTo (m_TransitionIn); } }
My suspicion is that collision is now wonkier than usual since there was so much code dealing with colliders in the above mentioned Youtube tutorial. Since I'm not very familiar with what all the code did exactly... I can't be sure.
Also, I've attached the audio script to the player, as per the tutorial, but am not completely sure where to attach the coin script - the player or the coin? ( Just to clarify, I tried it on both, and neither made a difference in making it function)
Sorry for so many questions - I knew absolutely nothing about coding before starting this 2 days ago. Any ideas or suggestions are welcome! I can provide more info if needed. Thanks!
just a few pointers to make a checklist out of: 1. make sure the on collision script is attached to each coin gameObject 2. if you are using on trigger enter 2d, make sure the coin collision boxes are set to "IsTrigger" in the inspector view. 3. make sure the tag is exactly "Player" since "player" is a totally different tag (Happens to me all the time) 4. if none of these solve it, try placing the onCollision2D script on the player GameObject, but have it detect the opposite way, I.e
void onCollisionEnter2D (collision2D col){
if(col.gameObject.tag == "Coin") //or the tag assigned to coins
{
Destroy(col.gameObject);
print("nomnom");
}
}
Thanks for the tips! I went through and checked all the points you made. I also tried putting the code you gave into the player script, changing the tag to my coin tag, but still no luck. The coin remains and is stationary as a rock (I can't push it anymore, if that's relevant at all?).
I added another debug line to check if the coin can collide with things (obstacles, ground, or anything else). It turns out it is indeed colliding with the ground when I apply 1 gravity scale ins$$anonymous$$d of 0 to its rigidbody. BUT it is not colliding when the player jumps and bumps into it (with gravity back to 0 so the coin is not touching the ground during this test), even though it is stopping the player's movement, since nothing appears in the console when I do that.
I think there may be a problem specifically with how the player gameObject collides with things. $$anonymous$$aybe I'll need to dig into the code from the tutorial to find out how the player collider has been altered?
$$anonymous$$oving physics the wrong way is the cause in most cases. The player needs a rigidbody. $$anonymous$$ove it by changing the velocity in FixedUpdate, or in the case of a kinematic one, with $$anonymous$$ovePosition.
Answer by Mercbaker · Jul 02, 2017 at 06:13 PM
This is a simple problem and there is no "wonky" business going on. Triggers and collisions are pretty straight forward.
The mistake you are making is probably just your setup. I"ll post an example:
Player has a BoxCollider2D > Ridgidbody2D > tagged as "player"
Coin has BoxCollider2D > Checked as Trigger
Now just put this code into the coin class:
private void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.tag == "player") {
Destroy(gameObject);
}
}
The Above is an example for a Trigger, if you want to setup a Collision example then:
Add a Ridgidbody2D to the coin
Uncheck "isTrigger" in the coin's boxCollider2D
Add this code into the coin script
private void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "player") { Destroy(gameObject); } }
Your answer
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
isTrigger not working 2 Answers
Where to attach a script 1 Answer