- Home /
Targeting by clicking on them
Im trying to program my C# script do that i can target enemies by just clicking on them but all ive been able to do was program it so i cycles through enemies by pressing a button on the keyboard heres my script
using UnityEngine; using System.Collections;
using System.Collections.Generic;
public class Targeting : MonoBehaviour { public List targets; public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add(enemy);
}
private void SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
private void TargetEnemy()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
else
{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1)
{
index++;
}
else
{
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget()
{
selectedTarget.renderer.material.color = Color.red;
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget()
{
selectedTarget.renderer.material.color = Color.blue;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab));
{
TargetEnemy();
}
}
} If you can please tell me how to alter this script so i can target enemies by clicking on them Thanks in advance
Answer by akobylarek · Mar 14, 2012 at 12:47 AM
void Update(){
//Only search if clicked
if( Input.GetMouseButtonDown(0) ){
//Shoot ray from mouse position
Ray ray = Camera.mainCamera.ScreenPointToRay( Input.mousePosition );
RaycastHit[] hits = Physics.RaycastAll( ray );
bool gotTarget = false; //This is so we can deselect if we didn't click anything
foreach( RaycastHit hit in hits ){ //Loop through all the hits
if( hit.transform.gameObject.layer == 8 ) { //Make a new layer for targets
//You hit a target!
DeselectTarget(); //Deselect the old target
selectedTarget = hit.transform;
SelectTarget(); //Select the new target
gotTarget = true; //Set that we hit something
break; //Break out because we don't need to check anymore
}
}
if( !gotTarget ) DeselectTarget(); //If we missed everything, deselect
}
}
Thanks i wont be able to see if it works until tomorrow ill let you know if it works, and is the program$$anonymous$$g language your using C# i cant tell
its not working would the fact that im using first person have anything to do with it not working
Answer by Robomaster · Mar 16, 2012 at 12:00 AM
for some reason its not letting me write a comment, but its not letting me select any object in my game with the mouse and now Unity is saying error Object reference not set to an instance of an object Targeting.DeselectTarget () (at Assets/Scripts for game/Targeting.cs:76) Targeting.Update () (at Assets/Scripts for game/Targeting.cs:100)
My script is above so any help would be apprecitated thanks
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Selecting enemies with mouse button 0 Answers
Mouse targeting error 1 Answer
Enemy Select 1 Answer
What is wrong with my Code? (MouseDown event doesn't work) 1 Answer