- Home /
Help i need a way to target the enemy with tab key
my attack script i want to press tab and it select the target(Enemy)witch is a Gameobject if you can help finish my code ill be forever grateful:)
/// /// PlayerAttack.cs /// /// This is a basic attack script to get us use to usng C# and Unity /// /// Attach ths script to your player /// 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.Mouse0)) {
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) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
Answer by Azmodii · Sep 11, 2013 at 06:56 AM
There are two things that would need to happen here. Firstly, you would need to know what targets are on the screen and their relative distance from the player.
You can figure out what objects are visible to a certain camera by checking out this code. You simply include the C# script in your project.
Now we need get a list of GameObjects for this function to iterate through to figure out if they are visible. The easiest way is to tag enemies and then use GameObject.FindGameObjectsWithTag. Also see here for how to setup tags in the tag manager.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
Make sure to include the above at the TOP of your script, before the class declaration.
List<GameObject> AllEnemiesList = GameObject.FindGameObjectsWithTag("enemies");
List<GameObject> VisibleEnemiesList = new List<GameObject>();
This code will then get a list of all GameObjects tagged with this particular tag. Now we can check if these are currently being rendered by the main camera.
foreach (Transform Enemy in EnemiesList) {
if (Enemy.GameObject.renderer.IsVisibleFrom(Camera.main)) {
VisibleEnemiesList.Add (Enemy.GameObject);
}
}
Now we have a list which has all the visible enemies to the main camera!
Secondly, you would need to provide a function that executes when the tab key is pressed. You can do this by placing this code in Update ().
if (Input.GetKey (KeyCode.Tab)) {
//Do something here
}
Now I cant provide you the code here, you will need to decide how to sort the list so the target changes (Perhaps left to right? Perhaps by relative distance to the player IE closer enemies first?), but this will at least send you in the right direction :)
thanks alot ive added in the code but i get an error could you help me figure what im doing wrong :P i get this (Assets/_Scripts/random scripts/PlayerAttack.cs(17,54): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to
System.Collections.Generic.List')
i know for a fact i want it to select the targets closest to the player and deselect when i select the next
heres the script
/// /// PlayerAttack.cs /// /// This is a basic attack script to get us use to usng C# and Unity /// /// Attach ths script to your player /// using UnityEngine; using System.Collections; using System.Collections.Generic;
public class PlayerAttack : $$anonymous$$onoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
List<GameObject> AllEnemiesList = GameObject.FindGameObjectsWithTag("Enemy");
List<GameObject> VisibleEnemiesList = new List<GameObject>();
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
foreach (Transform Enemy in EnemiesList) {
if (Enemy.GameObject.renderer.IsVisibleFrom(Camera.main)) {
VisibleEnemiesList.Add (Enemy.GameObject);
}
} }
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$ouse0)) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Tab)) {
TargetEnemy();
} }
}
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) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
thanks agian for all the help
Hi,
Sorry I made a boo boo in my code!
Replace:
List<GameObject> AllEnemiesList = GameObject.FindGameObjectsWithTag("Enemy");
With:
GameObject[] AllEnemiesList = GameObject.FindGameObjectsWithTag("Enemy");
And then replace:
foreach (Transform Enemy in EnemiesList) {
if (Enemy.GameObject.renderer.IsVisibleFrom(Camera.main)) {
VisibleEnemiesList.Add (Enemy.GameObject);
}
With:
foreach (GameObject Enemy in EnemiesList) {
if (Enemy.renderer.IsVisibleFrom(Camera.main)) {
VisibleEnemiesList.Add (Enemy);
}
And with regards to selecting the closest enemy, you would need to iterate through this new VisibleEnemiesList and find which enemy was closest.
You would likely need another list that you could store these objects in order of distance, but there may be a better way of doing this.
looks better only erros i have is this and i forgot how to add them to the current context help once agian if possible
Assets/_Scripts/random scripts/PlayerAttack.cs(28,46): error CS0103: The name EnemiesList' does not exist in the current context and this Assets/_Scripts/random scripts/PlayerAttack.cs(51,33): error CS0103: The name
TargetEnemy' does not exist in the current context
/// /// PlayerAttack.cs /// /// This is a basic attack script to get us use to usng C# and Unity /// /// Attach ths script to your player /// using UnityEngine; using System.Collections; using System.Collections.Generic;
public class PlayerAttack : $$anonymous$$onoBehaviour { public GameObject target; public float attackTimer; public float coolDown; public int EnimiesList;
GameObject[] AllEnemiesList = GameObject.FindGameObjectsWithTag("Enemy");
List<GameObject> VisibleEnemiesList = new List<GameObject>();
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
foreach (GameObject Enemy in EnemiesList) {
if (Enemy.renderer.IsVisibleFrom(Camera.main)) {
VisibleEnemiesList.Add (Enemy);
}
} }
// Update is called once per frame
void Update () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$ouse0)) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Tab)) {
TargetEnemy();
} }
}
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) {
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}
thanks agian for all your help would there be another way to contact you for other help if not ill continue to use these forums
i reverted to my old script and finally got it working with some help thanks any ways your method just was not working for me