- Home /
Cannot detect collision between objects
Hi, I am making my first game and I do not really understand the collisions. I was trying to detect a collision between my character(charactercontroller) and the enemy's sword(rigidbody, mesh collider), using oncontrollercolliderhit, but it did not work. Could anyone help me?
The character's script
using UnityEngine;
using System.Collections;
public class PlayerStats : MonoBehaviour {
//public int LVL;
//public GameObject Target;
public int MaxHealth; // Максимальное количество здоровья
public int CurHealth; // Текущее количество здоровья
public int MaxMana; // Максимально количество маны
public int CurMana; // Текущее количество маны
public bool AttackStats = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(MaxHealth < CurHealth) CurHealth = MaxHealth;
if(MaxMana < CurMana) CurMana = MaxMana;
if(CurHealth < 0) CurHealth = 0;
}
void OnControllerColliderHit(ControllerColliderHit playerCollider){
if(playerCollider.gameObject.tag == "sword"){
Debug.Log("Hit Player");
}
}
}
Answer by BigRoy · Jan 02, 2014 at 09:33 PM
The OnControllerColliderHit() method is only called in combination with the Character Controller behaviour component.
Are you sure the object you're using contains the CharacterController behaviour component, because the OnControllerColliderHit method is called from that component (not from RigidBody component if I'm correct).
Also see the following links:
http://forum.unity3d.com/threads/29919-OnCollisionEnter-vs-OnControllerColliderHit
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnControllerColliderHit.html
Put a Debug.Log("Hit Controller");
directly in the method to see if it's finding any hit in OnControllerColliderHit() method in general.
The OnControllerColliderHit() only gets called if the controller is moving at the same time
Quote from here:
"If you read the docs, that's how it should work: "OnControllerColliderHit is called when the controller hits a collider while performing a Move." (emphasis is mine.) If means it only checks during CC.Move or CC.simpleMove.
To check for other stuff hitting you when it moves, just use a regular OnCollisionEnter." - Owen Reynolds
You might want to use the OnCollisionEnter() method instead if you always want to find any collision.
Otherwise you could try putting it in the OnCollisionEnter() method. Like so:
void OnCollisionEnter(){
if(playerCollider.gameObject.tag == "sword"){
Debug.Log("Hit Player");
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
OnControllerColliderHit counting twice towards score 0 Answers