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