1
\$\begingroup\$

Despite the absurdly high number of "OnCollisionEnter not working" threads around the internet, no one else seems to have had this specific problem.

To start off with, I have a spherical "shield" object around my ship, which has a shader designed to give off impact effects when hit by an object. To trigger the shader impact effect, you need to give it the position from which do the effect. So, the easiest way to do this is to give the shield a Collider and, whenever OnCollisionEnter is called, it sends the contact.point details to the shader. That's not the difficult part. The part that isn't working is that OnCollisionEnter is not being called when the laser bolt fired at the shield actually collides with the shield.

Both the shield and the laser bolt have Sphere colliders, both are not triggers. Only the shield has a rigidbody, which is not kinematic.

void OnCollisionEnter (Collision collision) {
    Debug.Log (Time.time);
    if (collision.transform.tag == "Impact") {
        Debug.Log ("go");
        //Does the shader stuff
        }
    }
}

That script is attached to the shield object. Not only is "go" never being logged, but neither is the Time, so OnCollisionEnter is not being called.

The odd thing about this scenario is that, OnCollisionEnter will be called by objects that begin inside the shield's collider - so if I start the laser bolt inside the shield, then the function gets called as usual. But when I start the laser outside the shield and Translate it into the shield, nothing happens. For the life of me, I can not work out what's going wrong with it.

\$\endgroup\$
  • \$\begingroup\$ Usually a moving object like a laser bolt would have a Rigidbody attached. Have you tried adding one? Depending on the speed of the bolt there might also be tunnelling occurring, which you can try to address by enlarging the colliders, tuning the physics timestep, or enabling continuous dynamic collisions. Any luck in any of those directions? \$\endgroup\$ – DMGregory Jan 9 '17 at 4:27
  • \$\begingroup\$ For now, for the sake of working out this issue, I've made the lasers travel at 0.25 units per Update frame, which immensely slow. But I did add a rigid body to the laser bolt and now, yes, it does call. What would've been causing it to not work when there was only 1 rigidbody? \$\endgroup\$ – DJpotato Jan 9 '17 at 5:40
  • \$\begingroup\$ > But when I start the laser outside the shield and Translate it into the shield . Why do you translate it's position instead of using Force or Velocity? \$\endgroup\$ – Aditya Yadav Jan 9 '17 at 10:02
  • \$\begingroup\$ A snapshot of your object covered with shield with inspector values will be helpful \$\endgroup\$ – Hamza Hasan Jan 9 '17 at 10:08
  • \$\begingroup\$ When a laser bolt is instantiated, it gets rotated so that it will fire in the correct direction, and then the laser has a script that translates it however much I want every frame. They will only ever go in a straight line so I figured there was no need to bring the physics engine into it. Would there be the specific advantages of using the physics system on a laser bolt? \$\endgroup\$ – DJpotato Jan 9 '17 at 10:14
1
\$\begingroup\$

You would need to add a Rigidbody, you constrain the simulation in all the axis for the rigidbody and use MovePosition from it instead of using translate.

https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

\$\endgroup\$
  • \$\begingroup\$ Thank you, yes adding a rigidbody to the Laser Bolt did cause it to work. Any idea why the the rigidbody was required for it to work? All the script references say that only one of the objects involved in the collision needs a rigidbody for it to work \$\endgroup\$ – DJpotato Jan 9 '17 at 13:52
  • \$\begingroup\$ @DJpotato I believe that's correct for the situation where the object without a Rigidbody is stationary. When the object with a Rigidbody moves into it, the physics engine can detect that overlap while it's processing the body's movement. But if the moving object has no Rigidbody, then the physics engine never processes its movement through space and doesn't see the overlap start. This also used to come with a substantial performance penalty every time we used transform properties to move a non-rigidbody collider. General rule: any moving collider should have a body to own its motion. \$\endgroup\$ – DMGregory May 17 '17 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.