- Home /
Please help with a simple collision.
I have tried every script out there on the internet. None of them work. All I am trying to do is have a collision between a cylinder and first person controller. Pretty much, when the Player hits the cylinder, the Log says something. That isn't what it is supposed to do, but all I want to do is get this to work. Please help.
My code:
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.tag == "Enemy"){
Debug.Log("fjeoijfoiejfoiejf");
}
}
This is any and all code I have for the entire thing.
Screenshot of unity (red text was edited in) ![alt text][1] [1]: /storage/temp/27333-src.jpg
Answer by Jeff-Kesselman · Jun 05, 2014 at 09:42 PM
Character Controllers are weird. They don't use the standard callbacks.
If you want to know if the charter controller has collided with an object you need to use an OnControllerColliderHit callback http://docs.unity3d.com/ScriptReference/CharacterController.OnControllerColliderHit.html
I just don't use them, I build my own controllers using real Unity physics.
Answer by AceDawg45 · Jun 05, 2014 at 09:59 PM
Hmmm.... Didn't work for me. My code was:
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "Enemy"){
Debug.Log("feee");
}
}
Also, I have the script attached to my character. Is this right?
Yes it should be fine on your character as long as it is on the same gameObject as the character controller
Try this to see if you are getting the collision:
function OnControllerColliderHit(hit : ControllerColliderHit){
Debug.Log("In controller collider");
if(hit.gameObject.tag == "Enemy"){
Debug.Log("feee");
}
}
Ugh. FINALLY. I got it. For now. It took me 3 HOURS for that. Unity is gonna be a lot more tricky than I thought.... I am hardcore C style programmer (C++ C# C) and this is a lot different.
Okay, new question, same idea, problem with collisions. No need to post new thread about it. But, I have a var named health (it is an integer set to 100). It is global, and inside the function (you wrote it in the comment) if statement, I have health = health - 10; That way, when you touch the enemy, you lose 10 health. Also, inside the Debug.Log I have your health displayed ins$$anonymous$$d of "feee". Well, I lose like 50 health every time I touch it. Do you know how this happens? Full code (now): var health = 100; //global variable
function OnControllerColliderHit(hit : ControllerColliderHit){
if(hit.gameObject.tag == "Enemy"){
health = health - 10;
Debug.Log(health);
}
if(health < 0){
Destroy(gameObject);
}
}
If you already knwo C/C#, DONT use UnityScript.
It will give you more pain with less erro info because it is so loosely typed and it lacks the structural support of a real language.
use C# you will be much happier.
I encourage people who are learning Unity to skip UnityScript and just go to C# for the same reasons.
Answer by T1Green · Jun 05, 2014 at 11:02 PM
Your script seems fine. What you are missing is the "Enemy"-tag on the cylinder.
At the screenshot your "Enemy1" is labelled as "Undefined" under tag.
Make a new one called "Enemy" if it doesn't exist already and then remember to add the tag to the cylinder.
That should make it work.^^
Yeah I didn't notice that until after Jeff posted his code. Thanks anyway!
Your answer
Follow this Question
Related Questions
Overlap Detection HELP!! 0 Answers
How to switch off two lights? 2 Answers
function OnCollosionEnter problems 1 Answer
Collide detection with tag [JS] 0 Answers
OnTriggerEnter Or OnCollisionEnter? 3 Answers