- Home /
Need some basic collision detection help
I'm not sure what I'm doing wrong here...I was trying to implement and test a basic player health system where if the player touched an enemy, they would lose HP. After going through numerous tutorials and looking at unity forums, I have found that I can't even do a basic collision test...and I decided to go incredibly basic. What I have right now is a plane with a cube(no rigidbody or anything else added) and a Capsule with a character input controller on it. I have a script in c# and one in javascript on both with a debug.Log function on them. Could anyone please help me out with this? I feel like an idiot.
The C# script looks like this:
using UnityEngine;
using System.Collections;
public class Collisionnnn : MonoBehaviour {
void Start () {}
void Update () {}
void onCollisionEnter(){
Debug.Log("Hit something!");
}
}
and the javascript one looks like this:
pragma strict
function onCollisionEnter(){
Debug.Log("Hit something!");
}
Do you have a collider applied to the objects? like a box collider.
Answer by DryTear · Apr 06, 2013 at 02:47 AM
Go to GameObject>CreateOther>Cube
and apply this script to it:
function OnCollisionEnter(c : Collision){
Debug.Log("Collided with "+c.gameObject.name);
}
function OnTriggerEnter(b : Collider){
Debug.Log("Triggered with "+b.gameObject.name);
}
create 2 cubes, one with isTriggered set true other one to false and see the results
Answer by Chronos-L · Apr 06, 2013 at 05:43 AM
Your code is alright. However, you will need to check the setup of your cube and capsule.
Look at the table at the bottom of http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
If you have a character and a cube, a typical setup for collision would be:
Character
Collider
Rigidbody: IsKinematic
Cube
Collider
Rigidbody
Your character will become a Kinematic-Rigidbody-Collider and the cube is a RigidBody-Collider, and based on the table, both of them will react to collision detection and the collision-related functions in their respective scripts will be run.
Your answer
Follow this Question
Related Questions
How to detect which side of my player has collided? 2 Answers
collisions will sometimes be detected and sometimes will not 1 Answer
Quick question about colliders 1 Answer
Collision in code 1 Answer