I have a simple combat script set up, but everytime the prefab hits the enemy the enemies health will not go down. I could use any help I’m at the verge of giving up on this I can’t figure it out.
public var path1 : Transform;
public var path2 : Transform;
public var enemySpeed : int = 6;
public var currentHealth : int = 1;
public var maxHealth : int = 1;
private var target : Transform;
private var controller : CharacterController;
function Start () : void
{
SetTarget(path1);
controller = GetComponent(CharacterController);
currentHealth = maxHealth;
gameObject.renderer.material.color = Color.red;
}
function SetTarget(newTarget : Transform) : void
{
target = newTarget;
}
function Update () : void
{
var lookAtPosition : Vector3 = new Vector3 (target.position.x, this.transform.position.y, target.position.z);
transform.LookAt(lookAtPosition);
controller.SimpleMove(transform.forward*enemySpeed);
}
function OnTriggerEnter (node : Collider) : void
{
if (node.transform == target)
{
if (target == path1)
SetTarget(path2);
else if (target == path2)
SetTarget(path1);
}
}
function ModifyHealth (change : int) : void
{
{
currentHealth += change;
if (currentHealth > maxHealth)
currentHealth = maxHealth;
else if (currentHealth <= 0)
Destroy(this.gameObject);
}
}
this is the bullet script
var shot : GameObject;
function Start()
{
}
function OnCollisionEnter(info : Collision) : void {
if(info.collider.tag == "Enemy"){
info.collider.SendMessageUpwards("ModifyHealth", -1);
}
}
function Update () {
if(Input.GetButtonDown("Fire1"))
{
var porjectile = Instantiate(shot, transform.position, Quaternion.identity);
porjectile.rigidbody.AddForce(transform.forward * 1500);
}
}
Have you tried taking out the void function types? I don't think they are required. Maybe, not relevant :P In any case, you will need to add in a whole bunch of Debug.Log("Stuff" + variables); to see whats going on.
– meat5000I don't understand could you please show me an example?
– Asukakitti09function OnCollisionEnter(info : Collision) { Debug.Log("Collision Detected"); if(info.collider.tag == "Enemy"){ info.collider.SendMessageUpwards("ModifyHealth", -1); Debug.Log("Enemy Hit"); } }
– meat5000Use Debug.Log as stated above to check whether or not the prefab actually does anything + make sure your colliders are set to trigger
– Commander_QuackersYour adding health, not subtracting it.
– HuskyPanda213