- Home /
How to get Enemy script to target only things in its trigger effect
hello, ive created a script that when an heroe gets within the trigger of a enemy the enemy would then be able to tell what heroe entered and then only damage the heroes that are in its trigger effect. But it doesnt seem to work, unity has given me a few errors, one of them saying that Distance has already been used and i dont really know how to alter my script so that it works. Please help, thanks in advance, heres my script
using UnityEngine;
using System.Collections;
public class EnemyAttack1 : MonoBehaviour {
public GameObject target;
public GameObject target2;
public bool Knightbool = false;
public bool Linemanbool = false;
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(attackTimer == 0) {
OnTriggerEnter();
attackTimer = coolDown;
if(attackTimer == 2.0f){
GetComponent<EnemyAttack1>().enabled = false;
}
}
}
}
void OnTriggerEnter(Collider Col) {{
if(Col.gameObject.tag == "Lineman"){
target = GameObject.Find ("Lineman");
Linemanbool = true;
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 < 100.5f) {
LinemanHealth lh = (LinemanHealth)target.GetComponent("LinemanHealth");
lh.AdjustCurrentHealth(-10);
}
}
if(Col.gameObject.tag == "Knight"){
target = GameObject.Find("Knight");
Knightbool = true;
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 < 100.5f) {
KnightHealth kh = (KnightHealth)target.GetComponent("KnightHealth");
kh.AdjustCurrentHealth(-10);
}
}
if(Col.gameObject.tag == "Knight" && Linemanbool == true){
target = GameObject.Find("Knight");
target2 = GameObject.Find("Lineman");
float distance = Vector3.Distance(target && target2.transform.position, transform.position);
Vector3 dir = (target && target2.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 100.5f) {
LinemanHealth lh = (LinemanHealth)target2.GetComponent("LinemanHealth");
lh.AdjustCurrentHealth(-10);
KnightHealth kh = (KnightHealth)target.GetComponent("KnightHealth");
kh.AdjustCurrentHealth(-10);
}
}
}
}
}
Please use the code tags when pasting code in here, or give a link to pastebin or something. It's nearly impossible to tell what your code is even trying to do when it gets improperly formatted because you didnt use the code tags. 99.9% of all of us programmers are going to look at this, and click the back button because they don't need to have to take the time to decipher the jumbled code just to try to help you. However, if you were to edit your post, and fix the code display you might actually get a good number of responses from people that will be willing to help you.
Please format your code correctly. Use the 101010 button (or indent every line 4 spaces). I would do it for you, if it wasn't that many lines...and the empty lines also don't help...
Sry guys it took me about 30 $$anonymous$$s to edit it becuase of the bugs in unity and i guess i didnt see the formatting errors sry.
Answer by by0log1c · Sep 07, 2012 at 04:16 AM
You are declaring a local variable named 'distance' twice in the OnTriggerEnter method. Idem for 'dir' and 'direction'. I honestly didn't read the code thoroughly to check if you intended to overwrite the previous value or what.
You should probably change all declaration except the first ones, from this:
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
to a simple assignment, to replace the previously stored values:
//just an example, note the lack of 'float' and 'Vector3'
distance = Vector3.Distance(target.transform.position, transform.position);
dir = (target.transform.position - transform.position).normalized;
direction = Vector3.Dot(dir, transform.forward);
OR just rename your variables so you don't use the same name twice.
ohhh okay thanks it thought i could only use direction and distance, i didnt know i could change the name. Also unity is saying you cant have && in overands of type, heres the code its saying that for
float distance = Vector3.Distance(target && target2.transform.position, transform.position);
using Vector3.Distance you can only use one variable in each field.
Example: float distance = Vector3.Distance(target.transform.position, transform.position);
If you need to find the distance between A to B, and from B to C, then you'll need to do two different Vector3.Distance checks accordingly.
okay so would i just change the float distance to "float distance1"
Your answer
![](https://koobas.hobune.stream/wayback/20220613080724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NavMesh problem it does not respond corectly 0 Answers
destroy a non-trigger object hitting a trigger object? 1 Answer
Enemy doesn't shoot 1 Answer
Object: collider to floor, trigger to player 1 Answer
Distance destroy object 3 Answers