Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wumbo9 · Jul 01, 2017 at 04:48 PM · scripting problemcollideraudiotrigger

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andycodes · Jul 01, 2017 at 05:40 PM 1
Share

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");
         }
 }


avatar image Wumbo9 andycodes · Jul 01, 2017 at 07:05 PM 0
Share

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?

avatar image hexagonius · Jul 02, 2017 at 04:52 PM 0
Share

$$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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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"

alt text


Coin has BoxCollider2D > Checked as Trigger

alt text

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);
             }
         }
    

setup02.jpg (157.5 kB)
setup01.jpg (168.9 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

isTrigger not working 2 Answers

Where to attach a script 1 Answer

Play audio only once in trigger collision entry? 1 Answer

Why isnt OnTriggerEnter Tags working. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges