- Home /
Trying to make gameObjects the target on right click
I have an array that adds enemies.
using UnityEngine;
using System.Collections;
using Pathfinding;
public class LookAt : MonoBehaviour {
//public Transform target;
public GameObject tank;
public GameObject turret;
public float NoDelay = 0;
public float fireDelay = 5.0f;
public float speed = 100;
public float slightDelay = 3f;
public GameObject bullet;
public float radius = 50.0f;
public GameObject bulletSpawn;
public LayerMask mask;
public float fireRate = 0;
//public Transform target;
private GameObject target;
void OnTriggerEnter(){
}
void Update(){
Collider[] enemyArray = Physics.OverlapSphere(transform.position, radius, mask);
if(enemyArray.Length > 0){
target = enemyArray[0].gameObject;
InvokeRepeating("targetingSystem", NoDelay, NoDelay);
InvokeRepeating("Shoot", fireRate, slightDelay);
}
if(enemyArray.Length < 1){
turret.transform.rotation = tank.transform.rotation;
target = null;
Invoke("OutOfRange", NoDelay);
}
}
void targetingSystem(){
turret.transform.LookAt(target.transform.position);
}
void Shoot(){
GameObject instance = Instantiate(bullet, bulletSpawn.transform.position, turret.transform.rotation) as GameObject;
if(instance.rigidbody)
{
instance.rigidbody.AddRelativeForce((Vector3.forward).normalized*speed*Time.deltaTime,ForceMode.VelocityChange);
}
Invoke("OutOfRange", NoDelay);
}
void OutOfRange(){
CancelInvoke("targetingSystem");
CancelInvoke("Shoot");
}
}
Id like to make it so if you right click on an enemy, they become the target. Anyone know how to do this?
Answer by perchik · Sep 06, 2013 at 01:34 AM
In general, I know the shape of the answer, but I'm not entirely sure about the code.
What you want to do is on Update check to see if they right clicked. If they did, do a raycast from the mouse position to see if they hit any of the objects that can become the target. Unfortunately, Raycasting is something I know conceptually, but has always been handled by someone else on my team...
void Update(){
if( Input.GetMouseButton(1)) //rightclick
{
//raycast
}
thats what i thought, but wasnt sure on how to do it.
Answer by RyanZimmerman87 · Sep 08, 2013 at 07:22 PM
Hard to tell if this is what you want exactly but here is an example:
//user clicks
if( Input.GetMouseButton(1)) //rightclick
{
//creates a ray from camera to mouse position
//creates a rayHit to detect the object hit
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit rayHit;
//casts ray 150 meters with rayHit to detect object hit
//could add layer masks as an additional condition after the distance
if (Physics.Raycast(ray, out rayHit, 150))
{
//can get position of ray hit for transforms/vector calculations
rayVector = rayHit.point;
//get the ray hit object
rayHitObject = rayHit.collider.gameObject;
//get the ray name or could also get the tag
stringRayName = rayHitObject.name;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613113648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiation array of enemies, c# ? 2 Answers
Why can't I set the Target 1 Answer
Cycle through an array? Have Camera Follow different Targets. 1 Answer
faster target selection 2 Answers