1
\$\begingroup\$

The "shot" is supposed to collide with the left bumper and then be destroyed, but the shot goes right through it. Both objects have a box collider.

The following is the code I am using:

#pragma strict
var speed : int = 2;
var collided_with : GameObject;

function Update () {
    transform.Translate(Vector3(-1 * speed * Time.deltaTime, 0, 0));
}

function OnCollisionEnter (col : Collision) {
    if (collided_with.tag == "Left") {
        Destroy(gameObject);
    }
}

I have made sure that all the tags are assigned correctly and that there are no spelling errors. The variable collided_with is also assigned to the left bumper. What am I doing wrong?

\$\endgroup\$
1
  • \$\begingroup\$ I would be aware of how fast is your item going as this can affect if it collides at high speeds. If the speed is too high then the next position update before it hits a "wall" is going to place the item behind the wall without a collision activating. \$\endgroup\$ Commented May 19, 2014 at 13:01

3 Answers 3

3
\$\begingroup\$

Collider is only one piece of the puzzle -- it detects a collision -- but each object in the collision should also have a Rigidbody attached if you want an actual collision response.

Also, depending on the speed, you may need CCD - Continuous Collision Detection.

\$\endgroup\$
0
\$\begingroup\$

You should consider using a RigidBody on some of your objects. There are 3 types of collision detection for a RigidBody component. Discrete is fastest, but it can result in the "bullet through paper" problem that you are having.

https://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-collisionDetectionMode.html

\$\endgroup\$
0
\$\begingroup\$

Maybe you should check if your rigidbody is set to kinematic, because if it is, then its immune to collision input. Which could then lead to this problem you are facing. To fix it simply set you rigidbody to dynamic.

\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .