Chris
Chris

Reputation: 28064

How should I detect 3 way collision in Unity 4.3

I am making a basketball game in Unity, using a trigger collider to detect when the ball passes through the net. At the time this trigger happens, I also want to check if the ball is in contact with the rim (2pts) or not (3pts, swish). I already have an OnCollisionEnter defined for the rim, but I want to know of this collision from the OnTriggerEnter function on the trigger collider itself.

So far, I have:

#pragma strict

// defined on the basket itself
function OnTriggerEnter(info:Collider) {
    if (info.name == "ball") {
        Debug.Log("Basket made");
    }
}

and then

#pragma strict

// defined on the rim
function OnCollisionEnter (info:Collision) {
    if (info.collider.name == "ball") {
        Debug.Log("Ball hit rim");
    }
}

I would like to detect the latter in the function of the former, some how.

Upvotes: 0

Views: 283

Answers (1)

Rudolfwm
Rudolfwm

Reputation: 681

You define two functions on the ball :

function HitRim()
{
    // do something here, like score + 2
}

function HitBasket()
{
    // do something here, like score + 3
}

and change your code to:

// defined on the rim
function OnCollisionEnter (info:Collision) {
    if (info.collider.name == "ball") {
        Debug.Log("Ball hit rim");
        var lBall: YourBallClass;
        lBall= info.gameObject.GetComponent("YourBallClass");
        lBall.HitRim();
    }
}

and

// defined on the basket
function OnTriggerEnter(info:Collision) {
    if (info.collider.name == "ball") {
        Debug.Log("Ball hit basket");
        var lBall: YourBallClass;
        lBall= info.gameObject.GetComponent("YourBallClass");
        lBall.HitBasket();
    }
}

Upvotes: 1

Related Questions