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");
}
}
}
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).
“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: