Transform.Lookat only looks at list, not the nearest tagged
Hi! I'm really new to scripting and all, but i'm trying to get my object to look at the nearest object with a specific tag! however with this code i've written it's only looking at the nearest object from a list, say for example, its looking at "SoulShard", then "SoulShard (1)", "SoulShard (2)", etc. instead of the nearest soulshard with the tag, could yall help me out?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LookAt : MonoBehaviour
{
private Transform target;
public Transform playerObject;
private float distance;
private float minimumDistance = 5f;
void Start()
{
}
private void Update()
{
target = GameObject.FindWithTag("MinimapFinder").transform;
distance = Vector3.Distance(target.transform.position, transform.position);
if (distance <= minimumDistance)
{
playerObject.transform.LookAt(target);
}
Vector3 direction = target.position - transform.position;
}
}
Answer by streeetwalker · Sep 20, 2020 at 05:57 AM
@Vanderley5974, stick with it - it takes some time to get fluent enough that you will feel that you can solve any problem - but they still take work. (I don't know about being "good at it").
It seems like you want to get all gameObjects with a specifc tag and find the nearest one?
If so, then you have 2 separate problems: 1.) How to get a list of all objects with a specific tag. 2.) how to find the one in the list nearest to you.
GameObject.FindWithTag will only get you 1 random gameObject that is tagged with the Tag you specify. There is no way using that function to get all gameObjects with a Tag.
Furthermore, any of the Find operations are expensive in terms of processing and you're better off avoiding them where ever possible. If you have a small game with few objects it isn't an issue. In a large game with a lot of objects it can be a major issue.
To solve problem #1 you need another strategy. It is nearly always a good idea to keep references to all objects in your game using an array or list by building the list upfront, and if you are dynamically adding objects add a reference to each new object when you instantiate them. If you've place the objects into the game manually declare the array/list as a public variable and add them to the script's inspector field.
To solve problem #2, iterate over the list use any of the Find Smallest Value algorithms, such as this example:
//assume all of the gameObjects in the is list are tagged with your specific tag
// example list declaration
List<GameObject> happyGameObjects = new List<GameObject>();
//assuming the list is populated:
float minDistance = Mathf.Infinity;
GameObject foundGameObject = null;
for( int i= 0; i < happyGameObjects.Count; i++ ) {
GameObject currentGO = happlyGameObjects[ i ];
// The next two lines are less expensive than the Distance function
// assumes this script is on the object we are measuring from
// these two lines can be combined
float direction = currentGO.transform.postition - transform.position;
float distance = direction.sqrMagnitude;
// record the shortest distance
if( distance < minDistnace ){
minDistance = distance;
foundGameObject = currentGO;
}
}
// that's it - the nearest gameObject is referenced in foundGameObject when the loop is done
(Note that I am not using foreach to iterate over the list because it too is expensive, especially if your doing mobile games, so as a matter of practice I avoid it where ever possible.)
There are certainly many ways to do both steps 1 and 2.
i'll see what i can do, many thanks! =) I'm pretty tired right now and it's late and I just wanna get it working, so i'll figure something out
Your answer
![](https://koobas.hobune.stream/wayback/20220613012205im_/https://answers.unity.com/themes/thub/images/avi.jpg)