- Home /
Unity 3D C# - Collosion Code Not working?
This is my code
using UnityEngine;
using System.Collections;
public class ActivateKill : MonoBehaviour {
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "Killer")
{
//Destroy(col.gameObject);
Debug.Log("COLLISION!");
}
}
}
Not sure what the problem is. The script is applied to the first person controller with a capsule collider with a capsule mesh, the capsule then collides with the cube, it is meant to show in the log that It has collided. but when it collides nothing happens nut sure, any suggestions?
Answer by HarshadK · Apr 10, 2014 at 06:19 AM
Ok! Here are the solutions.
There are two ways to achieve this:
1) OnControllerColliderHit()
2) OnTriggerEnter()
Method #1:
The function OnControllerColliderHit() is available for the controller.
Code with that is:
void OnControllerColliderHit (ControllerColliderHit collision) {
if(collision.gameObject.name == "Killer"){
Debug.Log("COLLISION!");
}
}
But this is fired not just on controller enters the collision but also on stay.
Method #2:
Make your collider for controller object as a trigger.
Code is:
void OnTriggerEnter(Collider collider)
{
if(collider.gameObject.name == "Killer")
{
//Destroy(collision.gameObject);
Debug.Log("COLLISION!");
}
}
This will be fired once only when the collision is entered and not on stay.
As you are destroying the game object on collision, I suggest method #2 with trigger. Also OnControllerColliderHit() will be called continuously if you are walking on a floor which is just not required in this case.
Cheers!
no it didn't solve it for some reason, I put a rigedbody on the onto the FPC, then I froze the x,y,z so then the FPC doesn't fly in the air, and the cube is named "$$anonymous$$iller", i'm so confused, is it because its in C#?
$$anonymous$$ethod 1 works, thank you very much, now I know I have to use the colliderhit method
Answer by HassanBajwa · Apr 10, 2014 at 09:37 AM
Add RigidBody to your first person controller and don't forget to Freeze its position to X,Y,Z.
I did that, same result, bit confused why its not working, does unity have some sort of cache I need to clear? I don't see any reason why this is not working, I put a ridgedbody on the cube and the FPC froze both X,Y,Z, script is put of the FPC, which has a capsole collider
Then there might be problem with "$$anonymous$$iller" object. You may try it with tag give a tag to object. and try collision.gameobject.tag = "";
Answer by NDJoshi · Apr 10, 2014 at 09:22 AM
No, Your problem has no relation with any programming language (C#). I think you need to check your "killer" object's property. Is your object having rigidbody or not? And if your is kinematic is checked or not? Go through this, and i think it will solve your problem. Just let me know whether your problem is solved or not.
Your answer
Follow this Question
Related Questions
Camera raycast? 2 Answers
Can I choose the right collider? 1 Answer
How can I detect which SIDE of a box I collided with? 3 Answers
How to check if an object is colliding with another? 3 Answers