can I use 'ref' here to get current position of selected Game Object
Hi. In my game I have the case that at one point the Camera starts following a certain GameObject. Depending on the value of a certain variable there is a list of 14 different GameObjects the Camera chooses to follow. I wrote a working Method Follow(Vector3 Pos, Quaternion Rot)
which makes the camera following smoothly the game Object. So far so good. I could easily execute the switch-case statement below in the Update()
method, but my in opinion that is unnecessary and a waste of ressources since once the camera starts following one GameObject it remains with this GameObject. So I want to use something like this ref camPos=firstGO.transform.position;
so camPos
updates automatically when the transform.position of the GameObject changes. How can I realize this?
switch (variable)
{
case 1:
camPos=firstGO.transform.position;
camRot = firstGO.transform.rotation;
break;
case 2:
camPos= secondGO.transform.position;
camRot = secondGO.transform.rotation;
break;
// 12 more cases to come ....
}
void Update()
{
if (startFollow) //Set true by an event
{
Follow(camPos,camRot); //Method I have written which works fine
}
}
Answer by doublemax · Sep 05, 2016 at 04:05 PM
Just pass the transform from the GameObject to want to follow to the "Follow" method. It's already a reference and you will always get the "live" position/rotation from it.