Target closet object with tag and get transform?
What I'm trying to do is detect if the object with a certain tag is within range and if it is rotate the gun to face the targeted object. What I have now works on 1 object by name, but can't figure out how to detect the object if it has another name. I have tried to use findobjectswithtag but am having no luck.
using UnityEngine;
using System.Collections;
public class LaserLook : MonoBehaviour {
public Transform target;
public float objectDistance;
public float rotationDamping;
void Start () {
target = GameObject.Find ("Random Material").GetComponent<Transform> ();
}
void Update () {
objectDistance = Vector3.Distance (target.position, transform.position);
if (objectDistance < 20f)
{
lookAtPlayer();
}
}
void lookAtPlayer()
{
Quaternion rotation = Quaternion.LookRotation (target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
}
Answer by Positive7 · Mar 16, 2016 at 08:45 PM
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public GameObject[] target;
public float objectDistance;
public float rotationDamping;
void Start()
{
target = GameObject.FindGameObjectsWithTag("Enemy"); //It will look for all objects tagged as "Enemy"
}
void Update()
{
for (var i = 0; i < target.Length; i++)
{
objectDistance = Vector3.Distance(target[i].transform.position, transform.position);
if (objectDistance < 20f)
{
lookAtPlayer();
}
}
}
void lookAtPlayer()
{
for (var i = 0; i < target.Length; i++)
{
Quaternion rotation = Quaternion.LookRotation(target[i].transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
}
}
Ok, I ran into a problem with this code. The code works great if there is only 1 object with tag, but if there are multiple objects the axis no longer points at the object I'm flying toward. Here is a video capture of whats happening. The Random $$anonymous$$aterial on the left and the Random Object on the right both have the same tag. When I fly toward one the axis points away from the object ins$$anonymous$$d of toward. Not sure what the problem here is. Please help. Thank you. link text
Hmm... the code should look for the closest object so it should work, but I'll double check it
Try this :
using UnityEngine;
public class LaserLook : $$anonymous$$onoBehaviour
{
public float rotationDamping;
public float objectDistance;
public GameObject[] target;
GameObject FindClosestAsteroid()
{
target = GameObject.FindGameObjectsWithTag("Asteroid");
GameObject closestTarget = null;
var distance = $$anonymous$$athf.Infinity;
var position = transform.position;
foreach (GameObject go in target)
{
Vector3 distanceDiff = go.transform.position - position;
float curDistance = distanceDiff.sqr$$anonymous$$agnitude;
if (curDistance < distance)
{
closestTarget = go;
distance = curDistance;
}
}
return closestTarget;
}
void Update()
{
var distanceFromTarget = Vector3.Distance(transform.position, FindClosestAsteroid().transform.position);
if (objectDistance > distanceFromTarget)
{
RotateLaser();
}
else {
RotateLaserDefault(); //Reset Laser position to zero
}
}
void RotateLaser()
{
Quaternion rotation = Quaternion.LookRotation(FindClosestAsteroid().transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationDamping);
}
void RotateLaserDefault() {
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, Time.deltaTime * rotationDamping);
}
}
Yes. Thank you sooo much. Now it is working the way it should.
Your answer
![](https://koobas.hobune.stream/wayback/20220612072111im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Targetting Script Errors 0 Answers
Tower defence targeting first enemy 0 Answers