Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

OnCollisionEnter basics

Discussion in 'Scripting' started by edplane, Nov 14, 2009.

  1. edplane

    edplane

    Joined:
    Jul 13, 2009
    Posts:
    485
    Hi, I jus want to know more about OnCollisionEnter. See, triggers work fine to a certain degree, but the main problem is, triggers have no collider, eg, any object can move through them. So, colliders is the obvious choice. However, I have a problem. Example:
    Code (csharp):
    1.  
    2. var sound : AudioClip;
    3.  
    4. function OnCollisionEnter()
    5. {
    6.     audio.PlayOneShot(sound);
    7. }
    That dosent work for some reason. seeing some scripts on OnCollisionEnter, i see that its something like:
    Code (csharp):
    1. OnCollisionEnter(Collider : Collider)
    But, what EXACTLY does the (Collider : Collider) Do? Whats it for? Whats it link to? Thanks in advance. :D
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    The Collider class is a class for all colliders. BoxColliders, SphereColliders, etc... are all sub-types of the Collider class. It holds a connection to the collider's transform, gameObject, and much more. For more details you can search for it inside the scripting reference.
     
  3. JaminFrederick

    JaminFrederick

    Joined:
    Feb 14, 2018
    Posts:
    2
    I wasn't sure where to post this, since there are many questions regarding collision/trigger events. But I thought this could help people out.

    I have used Unity's collision system many times, but I find myself always having to remind myself when starting over again from scratch with two primitive objects (each containing at least a collider), about what components to add where (at a minimum) when trying to get either a collision or trigger to occur, and how that will have a (physical) effect on the object. So, I did a small experiment with the following results.

    I created a cube and a sphere (they already each contain a collider by default), intersected halfway with each other near the origin (to see the result of a physical collision move), and attached this script to each object, to see what would happen:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DoCollide : MonoBehaviour
    4. {
    5.     private void OnCollisionEnter(Collision collision)
    6.     {
    7.         GetComponent<MeshRenderer>().materials[0].color = Color.green;
    8.     }
    9.  
    10.     private void OnTriggerEnter(Collider other)
    11.     {
    12.         transform.localScale = transform.localScale * 1.0001f;
    13.     }
    14. }
    I used scale (as opposed to color) for the trigger, to make sure by inspection that both collision call and trigger call never happened at the same time (they didn't).

    I thought this had some interesting results and might be of use to others out there too. Let me know if anything looks amiss here, or if it in fact reflects what you would expect from the Unity physics engine.

    Collision0.PNG

    Collision1.PNG

    CollisionChart.PNG
     
    JoelLeeJie likes this.