- Home /
Help with simple collision problem.
Ok I know this is probably a simple solution to this. And I tried to solved myself.. But I need some help before my head explodes, or I have a stroke. I more of a graphics guy....
I working on very simple collisons. Basically I'm using the "First Person Controller" for the player I have added a simple cube(From Unity). I added Rigidbody to the cube. Basically I am trying to get it when so that when the player collides with the box. A message would display in the debug log.
I have written this script (javascript) and added it to the cube.
Code:
function OnCollisionEnter(collision:Collision){
if (collision.gameObject.tag == "Player")
{
print ("yay it works");
}
}
I get no errors, but I also get no display in the console/debug log. So basically nothing is happening. Is their something wrong with the code? Can I get some help please?
Please learn how to correctly format your code snippets (fixed for you this time).
Answer by Mortim · Dec 28, 2009 at 09:49 PM
You should probably verify that your player object has a collider whose configuration allows collision detection with the collider associated with your cube. Search for the "collision action matrix" (in the middle of the page) in the link below :
http://unity3d.com/support/documentation/Manual/Physics.html
Answer by jmorga · Dec 28, 2009 at 11:09 PM
Yep I figured it out I need to add a box collider, thanks for the help everyone... For some reason I thought a Character Controller was enough.
It's good practice to accept the answer that solved your problem (so it will get moved to the top) and thank in a comment ins$$anonymous$$d of an answer to keep everything nice and clean as UnityAnswers is intended to be sort of a database =) Greetz,
Answer by Ehren · Dec 28, 2009 at 10:47 PM
Make sure your player has a collider, and that the Is Trigger checkbox on the collider is unchecked. Also make sure that the rigidbody attached to your cube is not kinematic (i.e. that Is Kinematic is unchecked).
Then try changing your code to the following:
function OnCollisionEnter(collision:Collision){
Debug.Log("Collided with: " + collision.gameObject.tag);
}
If everything is working right, you should see the tag value of any object that collides with the cube printed out to the console window.
Your answer
Follow this Question
Related Questions
How to make my moving object bounce 2 Answers
How to make an arrow to become stuck? 1 Answer
Continuous collision detection - unnatural behaviour 0 Answers
Collision Destroy script without trigger 0 Answers