Alan Fletcher
Alan Fletcher

Reputation: 283

Allow objects to pass through an object while also detecting the collision in Unity3D

I am trying to get a ball to pass trough an empty object but also send back a message to debug window. But I don't have a clue how to do this or where to start, so any help on this would be greatly appreciated. I am sorry I have no code to show as an example, however I have been able to get a collision detection or to allow the object pass through the empty object one at a time but never both. I have used OnTriggerEnter and OnCollionEnter.

Upvotes: 0

Views: 3077

Answers (2)

user2085599
user2085599

Reputation:

You want to make your GameObject's collider a Trigger on the Editor. Go to the editor and add a collider to your GameObject, then make it a Trigger.

C# Code

void OnTriggerEnter(Collider other)
{
    Debug.Log("I hit something: " + other.gameObject);
}

Javascript

function OnTriggerEnter (other : Collider) 
{
    Debug.Log("Hey I hit you: " + other.gameObject);
}

Upvotes: 0

Daniel A.A. Pelsmaeker
Daniel A.A. Pelsmaeker

Reputation: 50336

Put a Collider (e.g. a SphereCollider) on your empty object, and set its Is Trigger to true. Now you can use OnTriggerEnter in your script (attached to the empty object) as you expected.

public class MyBehaviour : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        var collider = other.gameObject;
        // Do something...
        Debug.Log(collider);
    }
}

Upvotes: 2

Related Questions