Problem with enemy that should attack

Hi,

my name is Kevin and I’m new to the forum and to unity.

I’m pretty sure that this question has already been asked several thousand times, but I cannot find the solution…

As I mentioned above, I’m a newbie with the engine, but I think it’s great. However, I’m facing some problems:

I’m currently walking through the tutorial of burgzerg (I think most of you know it…)

and with the second part of the “melee tutorial” I have some trouble:

The Tutorial goes like Script in C# for the Enemy to attack our “player”

using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour {
	public GameObject target;
	public float attackTimer;
	public float coolDown;
	
	
	// Use this for initialization
	void Start () {
		attackTimer = 0;
		coolDown = 2.0f;
	}
	
	// Update is called once per frame
	void Update () {
		if (attackTimer > 0)
			attackTimer -= Time.deltaTime;
		
		if(attackTimer < 0)
			attackTimer = 0;
		
			
	}
	
	private void Attack () {
		float distance = Vector3.Distance (target.transform.position, transform.position);
		
		Vector3 dir = (target.transform.position - transform.position).normalized;
		
		float direction = Vector3.Dot (dir, transform.forward);
		
		Debug.Log (direction);
		
		if (distance < 2.5f) {
			
			if (direction > 0) {
				PlayerHealth eh = (PlayerHealth)target.GetComponent("PlayerHealth");
				eh.AddjustCurrentHealth (-10);
			}
		}
		
	}
	
}

I typed this from the screen, while watching the tutorial (in fact, I did as he himself, copy&paste it from another script, written before, the following) In his video all seems to be working, but not with my scripts:

using UnityEngine;
using System.Collections;

public class PlayerAttack : MonoBehaviour {
	public GameObject target;
	public float attackTimer;
	public float coolDown;


	// Use this for initialization
	void Start () {
		attackTimer = 0;
		coolDown = 2.0f;
	}
	
	// Update is called once per frame
	void Update () {
		if (attackTimer > 0)
			attackTimer -= Time.deltaTime;

		if(attackTimer < 0)
			attackTimer = 0;

		if (Input.GetKeyUp (KeyCode.F)) {
					if (attackTimer == 0)
					Attack ();
					attackTimer = coolDown;
				}
			}
		
		private void Attack () {
		float distance = Vector3.Distance (target.transform.position, transform.position);

		Vector3 dir = (target.transform.position - transform.position).normalized;

		float direction = Vector3.Dot (dir, transform.forward);

		Debug.Log (direction);

		if (distance < 2.5f) {

					if (direction > 0) {
							EnemyHealth eh = (EnemyHealth)target.GetComponent ("EnemyHealth");
								eh.AddjustCurrentHealth (-10);
						}
			}
	
	}

}

I cannot make this work.
I don’t get an debug error or something like that. This script just seems not to affect the second one PlayerAttack.
It’s probably easy, but I didn’t spot any difference while watching the video again and again it totally looks the same to me…

edit: I just realised that not receiving any error messages should indicate that nothing is wrong with my code, am I right? I fixed one or two typos in the quote and checked whether they were in the actual Code, which they weren’t so I still have this issue and seem unable to fix it…

best wishes,
and thanks in advance for your answers,

Kevin

that’s disappointing:

I have rebuilt the whole scene (which was luckily not that much :-D) and it still doesn’t work.
So I guess I hit a serious bug here? Now I’m at university and will probably check later, if I missed any other things.

Any help or hint would be appreciated,
so far, nothing new,

Kevin

You’re not setting the target field in either script. Are you supposed to set it from the Editor, and are you in fact setting it to whatever value you’re supposed to set it?

I set the target of the enemy by dragging the player into the target field in the inspector of the enemy.
WHen I’m home I’ll try to assign the target in the script so that right at the beginning the enemy has the target…

I did the same as in the tutorial (he also drags the Player into this field …)

thanks for your answer :slight_smile:

So I tried it and it still does the same thing… The script works, but the attacking or better the damaging is not done.

Here is my modified script:

using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour {
	public GameObject target;
	public float attackTimer;
	public float coolDown;

	void Start() {
		attackTimer = 0;
		coolDown = 2.0f;
		target = GameObject.FindGameObjectWithTag ("Player");
	}

	void update() {
		if(attackTimer > 0)
			attackTimer -= Time.deltaTime;
	
		if(attackTimer<0)
					attackTimer = 0;
		if(attackTimer == 0){
			Attack();
			attackTimer=coolDown;

			}
	}

	private void Attack(){
		float distance = Vector3.Distance(target.transform.position, transform.position);

		Vector3 dir = (target.transform.position - transform.position). normalized;

		float direction = Vector3.Dot(dir, transform.forward);

		if(distance < 2.5f){
			if(direction > 0){
				PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
				ph.AddjustCurrentHealth (-10);
				}
			}
		}
}

I did only change a few things:

  1. I changed the Attack() function so that it won’t look for a target by waiting for me to drag&drop it, but to make as target the “Player” Tagged GameObject (which is the player itself …)

If anyone can possibly give me a hint on how to fix this issue I’d really appreciate it.
It must not necessarily be with this script, but might as well be with another idea …

PS I tried commenting several things out

such as the line

“AttackTimer stuff in void update” which did not change anything, but the AttackTimer was not set (so everything cool here)

I than tried to modify the Attack()
by commenting out, the direction and distance aspects, which did not change anything either.

So I think it’s most probably not an issue with these things, but with

PlayerHealth ph = (PlayerHealth)target.GetComponent("PlayerHealth");
				ph.AddjustCurrentHealth (-10);

So what other way of decreasing the PlayerHealth in the script PlayerHealth

using UnityEngine;
using System.Collections;

public class PlayerHealth : MonoBehaviour {
	
	public int maxHealth = 100;
	public int curHealth = 100;
	
	
	
	public float healthBarLength;
	
	
	// Use this for initialization
	void Start () {
		
		healthBarLength = Screen.width /2;
		
	}
	
	// Update is called once per frame
	void Update () {
		
		AddjustCurrentHealth(0);
		
	}
	
	void OnGUI(){

		GUI.Box (new Rect(10,10, healthBarLength, 20), curHealth + "/" + maxHealth);   
		
	}
	
	public void AddjustCurrentHealth(int adj){
		
		curHealth += adj;
		
		if(curHealth <0)
			curHealth = 0;
		
		if(curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth <1)
			maxHealth = 1;
		
		
		healthBarLength =(Screen.width /2) * (curHealth / (float)maxHealth);
		
	}
}

could be found?

I hope you can answer these questions,

best wishes and thanks again in advance for response and help
Kevin

Maybe a typo from recreating the scripts, but in your last post, the update function is not called in the enemie’s script because it’s called update and not Update. It is considered as a completely different function and will not be called every fram by the engine.

awkward, I checked so seriously but that one slipped my eye …

Now it’s working (though I checked, the version before the void update was capitalized …)

I guess I would’ve tried and looked over again and again and well probably never have realized what actually was wrong …

(wrong is I guess not exactly correct, more it’s the wrong function that is called …)

thanks for your answer:-)

I hope things like that won’t happen any more