- Home /
How to find the transform of a GameObject from an array.
When the level starts the Main Camera should set the spawned player as its target and look at it. At game start I can't get the camera to find a Game Object with the tag "Player" and set that to Target. This isn't working though! I'm very new to c# so I could be missing something quite simple.
Any help would be greatly appreciated!
public class LookAtTarget : MonoBehaviour
{
public Transform Target;
[Range(0f,1f)]
public float rotationSpeed = 1;
void Start()
{
Target = GameObject.FindWithTag("Player").transform;
}
// Update is called once per frame
void Update ()
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
}
}
How is the player spawned? Is it already in the scene or something is created it?
Answer by dan_wipf · Nov 09, 2018 at 07:05 AM
well i would avoid this method if it’s possible. i’d recomend you just to manualy asign the player to your property.
but could it be that you taged nothing as player? or wrote it wrong? to find out it it finds nothing you could debug the function.
Debug.Log(GameObject.FindWithTag(“Player”).transform);
I was initially doing that using a public transform. However, now I have character selection implemented at the start of each level the selected character is spawned. I'm having trouble getting the spawned character and assigning it as the target for the main camera.
I've double checked the spelling of the tag and it matches the code.
Its possible that this code is being run before the selected character is spawned. $$anonymous$$aybe move the line to your Update: something like this?
public class LookAtTarget : $$anonymous$$onoBehaviour
{
public Transform Target;
[Range(0f,1f)]
public float rotationSpeed = 1;
void Update ()
{
if (Target)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
}
else
{
GameObject temp = GameObject.FindWithTag("Player");
if (temp)
{
Target = temp.transform;
}
}
}
}
I gave that a go but it still doesn't seem to work.
Answer by digiman72 · Nov 09, 2018 at 09:04 AM
[CODE]
myTransforms = new Transform[myResTags.Count];
for (int z = 0; z < myResTags.Count; z++)
{
if (myResTags[z] != null)
{
myTransforms.SetValue(myResTags[z].transform, z);///
// Debug.Log("objects with reqTags..." + myResTags[z]);
}
}
resultName = GetClosestEnemy(myTransforms).name;
[/CODE]
//////////////////////////// call is, .... resultName = GetClosestEnemy(myTransforms).name;
[CODE]
Transform GetClosestEnemy(Transform[] enemies)
{
Transform bestTarget = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = transform.position;
foreach (Transform potentialTarget in enemies)
{
Vector3 directionToTarget = potentialTarget.position - currentPosition;
float dSqrToTarget = directionToTarget.sqrMagnitude;
if (dSqrToTarget < closestDistanceSqr)
{
closestDistanceSqr = dSqrToTarget;
bestTarget = potentialTarget;
}
}
return bestTarget;
}
[/CODE]
a little more complicated for what you might need, but very usefull for finding closest target, the thing is to store whatever you want in the array into a list first, then set the values of element in the transform array, i used physics.overlap sphere to find objects of same tag in the radius, then i add them to a list, i set my transform array length or count to the list count, and then just set the values of each element, once you got your array, then you just call ex: target = GetClosestEnemy(myTransforms).name
Answer by Miso21 · Nov 09, 2018 at 04:57 PM
try with LookAt function.
public class LookAtTarget : MonoBehaviour
{
public Transform Target;
[Range(0f,1f)]
public float rotationSpeed = 1;
void Start()
{
transform.LookAt(Target);
}
// Update is called once per frame
void Update ()
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
}
}
I will try the LookAt function.
The public transform Target is never being assigned to the spawned rocket. I tried using the method Awake ins$$anonymous$$d of Start on the character spawner script however, that still didn't work.
Again I've double checked my na$$anonymous$$g conventions and there aren't any errors there.
Gah! What's going on?
try to assign camera
public class LookAtTarget : $$anonymous$$onoBehaviour
{
public Transform camera;
public Transform Target;
[Range(0f,1f)]
public float rotationSpeed = 1;
void Start()
{
camera.transform.LookAt(Target);
}
// Update is called once per frame
void Update ()
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.position - transform.position), Time.deltaTime * rotationSpeed);
}
Answer by RichardBDev · Nov 11, 2018 at 11:03 PM
So!
It turns out my initial script worked fine. Just a bug in unity. Once I had restarted few times it seemed to work just fine!
Thank you for everyone who helped.
Your answer
Follow this Question
Related Questions
How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 Answers
How to convert GameObject array to Transform array in C#? 2 Answers
Remove an object from an array and destroy it (C#) 2 Answers
Instantiate Game Object on Array of Transforms 1 Answer
Find all GameObjects with tag and add them to a transform array 2 Answers