My enemy is not taking damage...
I've tried to wrap my head around this for too long, so I'm just going to ask here :)
I've made an Enemy Script that's attached to the enemy in the scene: (The enemy script is called EnemyTest.cs and the enemy in the scene is called enemyPrefab.)
[code]
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EnemyTest : MonoBehaviour {
private int health, maxHealth;
public Image healthBar;
void Start ()
{
healthBar = transform.FindChild ("EnemyCanvas").FindChild ("enemyHealthBG").GetComponent<Image> ();
health = 100;
maxHealth = 100;
}
void Update () {
}
public void Hit(int damage)
{
health -= damage;
healthBar.fillAmount = (float)health / (float)maxHealth;
}
}
[/code]
In my bullet prefab, I have this script attached. Bullet1.cs:
[code]
using UnityEngine;
using System.Collections;
public class Bullet1 : MonoBehaviour {
float speed;
void Start () {
Destroy (this.gameObject, 2);
speed = 15;
}
void Update () {
transform.position += transform.forward * Time.deltaTime * speed;
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Enemy")
{
other.gameObject.GetComponent<EnemyTest> ().Hit (10);
Debug.Log ("Hit enemy");
Destroy (this.gameObject);
}
}
}
[/code]
The bullet prefab does not have a tag, but the enemy is tagged as "Enemy".
I have an enemy Canvas attached to the enemy too, with the health sliders and everything. Why is this not working? I followed a tutorial on youtube (https://www.youtube.com/watch?v=5zNDRMJ3_AA)
Any help would be REALLY appreciated. Cheers.
Does the Debug.Log("Hit enemy") show up?
Try making health a public variable so you can see if it changes in your scene.
Also the RigidBody on the bullet might need different collision settings because if it's moving too fast it might not recognize the collision. Try slowing it down.
Yeah thats the thing.. Te Debug.Log does show up. So the hit is being registred.
Answer by iagoccampos · Sep 05, 2016 at 02:14 AM
There are many factors that prevent "OnCollisionEnter " to be called... if the bullet is too fast, as @Benjames says, it will "jump" over the enemy collider. Check the layer collisison matrix on Edit > Project Settings > Physics. I don't know how is your game mechanic but take a look on OverlapSphere for your bullet or Raycast to player gun...
As I commented on @Benjames' post, the hit is being registred by the Debug.Log. So it should work? I dont really want to use RayCast as Im making a tower defense.
Answer by Dream_in_code · Sep 05, 2016 at 03:05 AM
//Try using OnTriggerEnter2D.Make sure the enemy box collider is triggered.
void OnTriggerEnter2D(collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
other.gameObject.GetComponent<EnemyTest> ().Hit (10);
Debug.Log ("Hit enemy");
Destroy (this.gameObject);
}
}
}
Ok, i will try this. I.m making a 3D game though.. Does that matter when using onTriggerEnter2D?
Answer by YoloJoe · Sep 05, 2016 at 07:46 AM
Would it be better to have the OnCollisionEnter statement in the EnemyTest.cs? And tag the bullet as "Bullet1" or something?
I found the issue. The enemy IS taking damage, however the healthBar is not decreasing. Anybody got a solution to this?
Your answer
Follow this Question
Related Questions
Health bar help 1 Answer
How would you tie a keypress to a fill amount? 1 Answer
Help with a health bar 0 Answers
Health bar/damage script not working. Please help. 0 Answers