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;
}
}
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.
Thanks for the replies, but I guess you misunderstood me.
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.
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.