How do I make a transform target a certen tag
So I have this script here
using UnityEngine;
using System.Collections;
public class CameraFallow : MonoBehaviour {
public Transform myTarget;
// Update is called once per frame
void FixedUpdate () {
if(myTarget != null)
{
Vector3 targPos = myTarget.position;
targPos.z = transform.position.z;
transform.position = targPos;
}
}
}
And I want it to target an object that has a certen tag, not just a defined object. How do I do that?
Comment
Answer by Fredex8 · Mar 01, 2016 at 07:29 AM
GameObject.FindGameObjectsWithTag
As it shows in the example that will return an array of all objects with that tag which you can then loop through.
Or if you know the tag is definitely unique you can use that which will return just a single gameobject.
Best practice is to define it in Start as shown in the example rather than calling it frequently as that is expensive.
Your answer
![](https://koobas.hobune.stream/wayback/20220612065748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate camera when colliding 0 Answers
Can someone help me with this camera orbiting script? 0 Answers
Transforms update problem 0 Answers
How do I optimize transforming vertices to screen space? 1 Answer