Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Unity: Prerequisites for functional 2D collision detection?

$
0
0
Hey everyone, happy Friday! Sorry this is probably in the wrong place - I didn't see any clear-cut way to make this for Unity (only option was Unreal, which I like but has nothing to do with my question lol). I've been ramming my head against the wall at 88 miles per hour trying to get to the future where I understand 2D collision detection in Unity (see what I did there? ). So basically, I've been trying to get a sound to play on collision, and failing miserably. After reviewing the API documentation dozens of times over, it occurred to me that the issue was not my audio code but the collision detection part. Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine; public class bounce : MonoBehaviour { public AudioClip[] clips; private AudioSource Audio; void Start() { Audio = GetComponent<AudioSource>(); } void OnCollisionEnter(Collision other) { if (other.gameObject.name == "top_boards") { Debug.Log ("Hey McFly you bojo! Those boards don't work on water!"); Audio.clip = clips [0]; Audio.Play(); } } } In the GUI, there are 2 sounds in "clips" and they both contain valid audio assets (".ogg" files, tested to confirm they work by temporarily setting "play on awake" to true). And audio stuff aside, just the Debug.Log should have worked. The game is basically Pong, and every scrap of info I found on Google basically told me to do what I did - but the event never seems to happen, because {reasons}. Then I found this: http://answers.unity3d.com/questions/657234/2d-object-collision.html Following this guy's suggestion, I made everything in my game a "Trigger" (whatever that means) and changed the event to " OnTriggerEnter", and suddenly the event started firing, because {why not?}. Of course the objects no longer MOVED on collision, so I kept fighting this stupid thing till I lost a few more locks of hair. Clearly, there are other aspects of this that I just haven't learned yet, some mysterious prerequisites that need to be done from the GUI before the code will run as everybody says it will, nobody seems to bother with explaining in their tutorials. Cryptic lingo like "Triggers" and "Kinematic" are as nonsensical as "Bojo" without some general context. Does "trigger" mean the object is able to trigger collision events? I didn't study quantum mechanics in high school, so noob-friendly English is preferrable (I'm no noob to C#, but the math behind physics is basically lost on me). Thanks!

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>