- Home /
Bug With Collision Detection In Unity 3?
Sorry if this seems to be a newbie question. However i seems to have problems detecting collision between my player and the gameObject i want to detect. Am i writing it wrong somehow?
function OnCollisionEnter(collision : Collision) { Debug.Log(" i got u");
if(collision.gameObject.Find("CarrotType1(Clone)") && getNos == 1)
{
Debug.Log("hello");
//Destroy(targets);
}
if(collision.gameObject.Find("CarrotType2(Clone)") && getNos == 2)
{
Debug.Log("hello");
//Destroy(targetz);
}
}
it does not even output the "I got you" message in the Debug. Thanks in advance for any suggestions that i might be doing it wrong. :)
I have been facing the same problem...if I drag an prefab into the scene it works, but when I respawn point, the instantiated object stops working as well.
@gabrielloke: this is not an answer to the question. I've converted your "answer" into a comment. If you want to ask your own question, post it as question an not as answer.
Answer by AudiOishi · Dec 10, 2010 at 10:27 PM
One thing to note: When using onTriggerEnter (this may also apply to collisions) the documentation says that both objects need either a collider or a rigidbody.
In my experience, this is not correct.
This is only true if BOTH Objects are moving.
If one of the objects is standing perfectly 100% still, then the other object will need to have a rigidbody as well.
Answer by Avery · Nov 03, 2010 at 02:31 AM
Thanks again for the suggestion. However i tried both ways. and it doesnt work also. ); This the whole code for this class. maybe its more informative. =/
class Collisions extends StageWaypoint {
public var targets : GameObject; public var targetz : GameObject;
private var thisNum : int; private var getNos : int;
function Awake() { }
function Start() {
//Debug.Log(targets); //Debug.Log(targetz); //Debug.Log("hellosssss"); }
function Update() { targets = GameObject.Find("CarrotType1(Clone)"); targetz = GameObject.Find("CarrotType2(Clone)"); getNos = thisNum;
//Debug.Log(getNos + " this is rec");
//Debug.Log(targets);
}
function OnControllerColliderHit(collision : ControllerColliderHit) { Debug.Log(" i got u");
if(collision.collider.name == "CarrotType2(Clone)" && getNos == 1)
{
Debug.Log("hello");
Destroy(targets);
}
if(collision.collider.name == "CarrotType2(Clone)" && getNos == 2)
{
Debug.Log("hello");
Destroy(targetz);
}
}
function recNum(no : int) { thisNum = no; }
}
Sorry for asking dumb questions, but is the script running on the object with the charactercontroller, and do you still not get "i got u" printed?
Yeps this script is attached to the object with the charactercontroller. and yeps again. i still dont get the "i got u" printed.
I've got a dumb question myself. xD What class(es) does StageWayPoint inherit from?
lols it inherit from the base class $$anonymous$$onoBehaviour =/
well.. i redid the whole project. and it works.... once. after that it stop detecting collision anymore.
Answer by Atnas1010 · Nov 03, 2010 at 02:00 AM
Have you attached a rigidbody to either the player or the carrots? And a collider to both of them?
Also, your if statement doesn't make that much sense. I would use something like
collision.collider.name == "CarrotType2(Clone)"
Does this solve the problem?
Thanks for the suggestion, however when i tried ur method. it returns an error that says,
Assets/Script/Collisions.js(40,30): BCE0019: 'name' is not a member of 'UnityEngine.Collision'.
Yeps, i have a character controller attached to my player, as well as a rigid body and a box collider attached to the carrots.
I don't know if this makes any sense. =/
Yesterday before i off the program. Everything was working fine. When i reopen the program the next time, usually all my collision detection failed.
Apparently, the OnCollisionEnter funciton itself is not even running when the game started.
function OnCollisionEnter(collision : Collision)
{
Debug.Log(" i got u");
}
I edited my script (mistake on my part, sorry) If the event still doesn't get called, you could try changing it to an OnControllerColliderHit(collision : ControllerColliderHit)
did you double check if the isTrigger flag on the box collider is turned off?
thanks, however either ways it doesnt seems to work. =/ i couldnt fathom out any problem with it at all. );
Answer by Proclyon · Nov 03, 2010 at 03:17 PM
Try using THIS Unity3D collision method. It should make life a bit easier:
It's a simple check for looking if the collision box of your moving charactercontroller has come into contact with something above below etc. Little else is needed for simple collision detection
Your answer
Follow this Question
Related Questions
OnCollisionEnter2D not being called on entering new tile once already called. 1 Answer
Latest 5.2.2p3 Patch Breaks 2D Collision? 13 Answers
Delayed Collisions Bug (includes video demonstration) 1 Answer
iOS simple shader does not work 1 Answer
Something went wrong in the Beast lightmapper: Duplicate name 1 Answer