Search Unity

[C# Scripting] OnCollisionEnter

Discussion in 'Scripting' started by menarlinaticy, Sep 26, 2016.

  1. menarlinaticy

    menarlinaticy

    Joined:
    Jun 16, 2016
    Posts:
    26
    Hey Unity3D users...

    How can i script collision detection in C# for Unity 5.3.5 3D ?... i am an beginner with Unity3D...

    I want to use collision detedction for the folow examples:


    Destroy gameObject or other objects:

    - Example1A: if the gameObject detect an collision... the gameObject is destroyed...

    - Example1B: if the first person character detect an collision of an specefic gameObject...
    the gameObject is destroyed

    - Example1C: if the first person character detect the collision of an specefic gameObject...
    the door goes open

    - Example1D: if the first person character detect the collision of an specefic gameObject...
    the gameObject has chanced to an other gameObject and the door goes open...




    Play an 3D Sound in the gameObject or other object:

    - Example1A: if the gameObject detect the collision of an specefic other gameObject...
    an 3D sound play whether the gameObject detect as axample the ground or wall...
    or any other gameObject...

    - Example1B: if the gameObject detect the collision of an specefic other gameObject...
    the gameObject is destroyed and play an 3D sound wether the gameObject detect
    this specefic gameObject...

    - Example1C: if the first person character detect an specefic gameObject... an 3D sound
    plays wether the first person character detect this gameObject... as example the ground,
    wall or other specefic gameObject...

    - Example1D: if the first person character detect an specefic gameObject... this specefic
    gameObject is destroyed and an 3D sound plays...



    This are the examples to help me with C# scripts for collision detection with the "OnCollisionEnter"
    function...

    Thanks, Jamie van Cadsand.
     
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Hey, so usually with collisions you should use Tags. You can set up tags using Tag Manager .
    Here is an example
    Each object should have it's own tag, depending of the functionalities you want to implement
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Assuming you understand @CloudKid idea of tags here's some examples of code:

    1A. This is the perfect design for bullets. They should destroy themselves, not expect others to.
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision) {
    2.          // Do anything you need to for a collison
    3.          // then:
    4.          Destroy(this);
    5. }
    1B. In general I prefer to have objects destroy themselves. But you might have an goal Object that the player touches and gets points for. If something else like a bullet hits it, nothing happens. Then you do need player to destroy it
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision) {
    2.       if (collision.gameObject.tag == "Prize Barrel")
    3.       {
    4.                 // all my code for winning a prize goes here
    5.                 Destroy(collision.gameObject);
    6.        }
    7. }
    1C. This requires that the door object have a public OpenDoor method
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision) {
    2.        if (collision.gameObject.tag == "Door")
    3.        {
    4.                // DoorScript is the name you gave to the script on the door
    5.                DoorScript script = collision.gameObject.GetComponent<DoorScript>();
    6.                // OpenDoor is a method in your door object's script
    7.                script.OpenDoor();
    8.        }
    9. }
    1D. I don't understand this one

    All the Playing sound questions are basically the same except you also call Unity's functions to play a sound. Which I'm not that familiar with. However, should be easy to google a tutorial on playing a sound
     
    CloudKid likes this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    tags is the easy starting point a lot of the tutorials go with... eventually you'll find tags limited, but you can move onto using "GetComponent" to check scripts and interface implementations attached to the gameobjects involved in the collisions. Basically OnCollision### usually needs some sort of "what did i hit" check before going off to do things. :D
     
  5. menarlinaticy

    menarlinaticy

    Joined:
    Jun 16, 2016
    Posts:
    26

    I try this script to activate the DoorScript:

    [/Code]
    {
    void OnCollisionEnter (Collision col)
    {
    if(col.gameObject.tag == "obj_seno");
    {
    col.gameObject.GetComponent<OpenDoor>();
    }
    }
    }[/Code]

    I tryed this, but it don't work...

    Can you help me... i don't how i can make an door script and even the collision detection
    don't not activate the DoorScript...
     
  6. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    I suggest you watch this video.
    TL;DR One of the collision objects needs to have a Rigidbody component. Do you have that?