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

How do you get around using OnCollisionEnter twice?

Discussion in 'Scripting' started by Haxxy28, Nov 4, 2019.

  1. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Basically I have this script:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MeteorCollision : MonoBehaviour
    6. {
    7.     public Transform craterpreFab;
    8.     void OnCollisionEnter(Collision collision)
    9.     {
    10.  
    11.         ContactPoint contact = collision.contacts[0];
    12.         Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
    13.         Vector3 pos = contact.point;
    14.         Instantiate(craterpreFab, pos, rot);
    15.         Destroy(gameObject);
    16.     }
    17.  
    18.  
    19.  
    20. }
    Which creates a crater wherever the meteor lands. However I have an object which essentially follows the player to clear things behind. It also needs to be destroyed if it lands on an object for now. I used this for other scripts:

    Code (csharp):
    1. void OnCollisionEnter(Collision collisioninfo)
    2. {
    3.     if (collisioninfo.collider.tag == "Obstacle")
    4.     {
    5.         Destroy(gameObject);
    6.     }
    7.     else if (collisioninfo.collider.tag == "Destroyer")
    8.     {
    9.         Destroy(gameObject);
    10.     }
    11. }[/CODE}
    12.  
    13. Which worked fine (just realised I could use OR but anyways...) however it doesn't work with this script as Oncollisionenter is being used to get the collision information. How do I use (Collision collisioninfo) and (Collision collision) in the same script. Just hoping someone can send me in the right direction to get around it, or just straight up tell me its not possible, but it probably is I just don't know how. Thank you for taking your time to read this.
     
    Last edited: Nov 5, 2019
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    First, fix your formatting, your last paragraph is inside the code tags, I was like "wut..." for a second before I saw that.

    I'm not clear on your problem, you have an object following the player, to clear.. 'things', it needs to destroy itself if it land on an 'object'..

    what are the things? what is the object?, what "doesn't work"?, what is the whole idea behind this?
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Both of those are of the same type, just with a different name. There's no semantic difference between these two things.
     
  4. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Hopefully this is clearer? (I wrote that late so i'm surprised it didn't make sense.)
    I re-wrote that bottom bit and tried to make it as clear as possible: (it won't change when I edit it)

    Which worked fine however it won't let me use both in the same script as Oncollisionenter is being used to get the collision information(Collision collisioninfo) but I also need to use (Collision collision) for where I want to place the crater. How do I use (Collision collisioninfo) and (Collision collision) in the same script. Just hoping someone can send me in the right direction to get around it.

    Additionally I meant to put: Which creates a crater wherever the meteor lands. However I have an object which essentially follows the player to clear things behind. So it needs to be destroyed if it touches "Destroyer" or lands on another object.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    "Collision collisioninfo" and "Collision collision" are exactly the same thing, just using a different name for the variable. If you wrote it as:
    Code (csharp):
    1. void OnCollisionEnter(Collision itsEasyAs123ABCYeahYouAndMeGirl)
    it is still the same thing, just the name of the variable you'd use in the rest of the function is different. But the variable names don't do anything as long as when you want to use that specific variable you use the name you chose.