- Home /
Combat begins when colliding with the enemy
So I have this code
void OnCollisionEnter (Collision col){
if (col.gameObject.name == "Enemy") {
combatStart = true;
}
}
// Update is called once per frame
void Update () {
//combat phase
if (Input.GetMouseButtonDown (0) && combatStart == true) {
enemyHealth -= (Random.Range (50, 125));
if (enemyHealth <= 0) {
enemyHealth = 0;
}
Debug.Log (enemyHealth);
Debug.Log (playerHealth);
}
}
In my mind, this makes sense, when the player collides with the "Enemy", and they are clicking MB 0 (LMB) they will begin to chunk the enemy health away. However, it's not detecting that I've collided with the enemy. Can someone explain to me why this is happening, and possibly propose a fix?
Answer by M-Hanssen · Jun 06, 2016 at 08:53 AM
Make sure both objects have colliders on them
Make sure you have a rigidbody attached on either one of the colliders
Your answer
Follow this Question
Related Questions
Player getting stuck in corners 0 Answers
How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers
Always Get Same Physics Collision Rebound Force 1 Answer
How can I make my character after hitting a wall? 1 Answer
How to Have Two Child Objects of One Parent Detect Collisions Between Eachother. 1 Answer