- Home /
OnCollisionEnter() not detecting collisions
Hi,
I have a small sphere that follows the player's cursor everywhere it goes, and I want an event to be called when that sphere hits an object in the world.
So far I have this code:
var cam : Camera;
function Update() {
var mousePos = Input.mousePosition;
mousePos.z = 175;
transform.position = cam.ScreenToWorldPoint(mousePos);
}
function OnCollisionEnter() {
Debug.Log("BAAA!");
}
This works fine for controlling the sphere's position, but for some reason the collisions are just not detected! I've tried adding a rigidbody as well, but still nothing happens! :(
Please help!
See this article that has a section on all of the things that need to be done to ensure collisions. You might want to use collision : Collision as a parameter.
I've tried all the things suggested in that article already, it just doesn't work! :( Perhaps this is because I am modifying transform.position directly from the script?
Because cursor moving too fast maybe your problem related to this question? http://answers.unity3d.com/questions/55179/cheapest-way-to-catch-collisions-on-very-fast-movi.html If that won't work, check the physics settings (layer vs layer matrix for collisions etc.)
Answer by NecrosDk · Oct 18, 2012 at 08:03 PM
Are any of your objects set to "Is Trigger"? If so they will not collide with your object.
Secondly, unless the sphere is a vital part of your graphics for the game, you should be using Raycasting to detect whether or not the cursor is hitting an object. There are plenty of tutorials on how to do that out there.
No, none of my colliders are triggers, and yes, the sphere is essential, I can't replace it with a Raycast as much as I would love to. :P
Too bad :/
And as you mentioned above, now that I think about it, directly changing the position of your object does not take collisions into account, read that somewhere.
So are there any alternatives, while still using a sphere and not a Raycast?
No it's fine to move a collider so long as it has a rigidbody attached - it is not ok to move a physics object by changing the transform outside FixedUpdate - but then it's not collisions, it's forces that go nuts. It is not ok to move a collider without a rigidbody.
I have a kinematic rigidbody attached. So do you think I should have the whole positioning code inside FixedUpdate()?
EDIT: FixedUpdate() doesn't work either. :(
Answer by Casper091 · Nov 03, 2012 at 08:29 AM
do you need to set the "Tag" for the objects and redo script to look for objects with the tag? im not sure, but its an option?
Answer by MibZ · Nov 05, 2012 at 10:22 PM
OnCollisonEnter/OnTriggerEnter only work for the collider attached to the object the script is attached to.
If your script is attached to your camera, it will only register collisions on the camera's collider.
Place your OnCollisionEnter code in a separate script and attach that script to the sphere, and voila.
Answer by Caviarail · Jan 25, 2013 at 10:56 AM
Did you ever consider attaching a character controller to your sphere? If you move the character controller instead (the character controller will move your object), it should collide with objects that have a collision box or a rigid body
this script worked fine for my start http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
Answer by EvansGreen · Jan 25, 2013 at 11:18 AM
Hi! I think it was you, I helped on the Unity Forums a few days ago, with the sphere following mouse problem.
You're using cam.ScreenToWorldPoint. This means your script is using a camera component. This means your script is attached to the camera, that you're using to detect the position of the mouse and convert it to world coordinates.
Your sphere is not detecting collision because you never told it to do so. You told YOUR CAMERA to detect it.
Create a separated script and attach it to the sphere GO, and use OnCollisionStay rather than enter. Collision detection and mathematical movement (transform) doesn't always work properly though, bear that in mind.
Good luck, and happy coding.
E.