Ball passing through triggered collider instead of stopping

Hello,

I have 2 soccer players who pass the ball to each other.

I created 2 triggered sphere colliders for each player and placed them in front of each one of them on the ground.

I am passing the ball using Rigidbody.velocity. When the ball collides with one of the sphere colliders, it should stop. Sometimes it works, but sometimes the ball just passes through the collider and continues rolling.

Here is a piece of the script attached to the ball :

//P1_Ball_Point is the sphere collider of player 1, and P2_Ball_Point is the sphere collider of player 2.

    void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.name == "P1_Ball_Point") {
                transform.position = P1_Ball_Point.transform.position;
                }

                if (other.gameObject.name == "P2_Ball_Point") {
                transform.position = P2_Ball_Point.transform.position;
                }
    }

And in the script of the player I wrote this:

void Update () {
                if (Input.GetKeyDown (KeyCode.Space) ) {
                        Ball.GetComponent<Rigidbody> ().velocity =
                        (target - Ball.transform.position) * smooth;
    //I assigned 2 for smooth
                }
    }

So how can I let the ball reach and stop at the target without letting it pass through the triggered colliders ?

Marking a collider as trigger makes it “not solid.” A classic use of a trigger is a supermarket door that opens when you stand in front of it. you cannot see the trigger (because usually they have no visible mesh attached) but your scripts can get info that something has entered or left the trigger area.

THEN, if you want something to “bump into” a solid boundary object, i.e., physically stop without entering, you need a different collider, it could be precisely in the same spot and everything, but it has to be marked as NOT a trigger.

GetComponent().velocity= Vector3.zero;

setting the position will not stop the ball,

Thanks for the replies, but I guess you misunderstood me.:smile:

Here is a short video of my problem:

I want the ball to stop as soon as it enters the triggered collider ( shown in the above video in the scene view). But sometimes it doesn’t work, as shown in the video.

in any cases if you need to stop it you have to set the velocity to Zero

is there any collider on the player ?

No just the model with animations

it could be better to have a collider !! but probably there is reason why you choose a trigger,

anyway did you tried to set the rigidbody.velocity of the ball to zero ?

Yeah but it is still sometimes doesn’t work and the ball keeps on rolling.:frowning:

May it be because of the velocity ?

I suppose there is a chance that the ball moves through the trigger before the physics engine has a chance to detect it. You can either fiddle around with the physics settings (which can have an effect on performance), or use a raycast to detect if there is a trigger in the direction that the ball is moving.

Try this

if you get the break in OnCollisionExit that means it’s bouncing who make the ball rolling again

so you will get to break after pressing space bar the first for the trigger and the second one for the collisionexit

void OnCollisionExit(Collision col)
{

    if(player != null)
    {
        Debug.Break();
    }
}

void OnTriggerEnter(Collider other)
{

    if (other.gameObject.name == "P1_Ball_Point") {

        player = other.gameObject;
        GetComponent<Rigidbody>().velocity= Vector3.zero;
        Debug.Break();
    }
   
    if (other.gameObject.name == "P2_Ball_Point") {
        player = other.gameObject;
        GetComponent<Rigidbody>().velocity= Vector3.zero;
        Debug.Break();
    }
   
}


void Update () {
    if (Input.GetKeyDown (KeyCode.Space) ) {
        Ball.GetComponent<Rigidbody> ().velocity =
            (target - Ball.transform.position) * smooth;
        Ball.player = null;
        //I assigned 2 for smooth
    }
}

This worked?