0
\$\begingroup\$

Essentially, I want to set the camera's Y position to another GameObject's Y position, but I only want to do this one. If you do

cam.transform.position = new Vector3(0, thing.transform.position.y, 0),

it will continuously update to that object's y. Even if I store the Y float in a variable and set the camera's y to that it still continuously updates. It is only supposed to happen once.

\$\endgroup\$
5
  • \$\begingroup\$ Can you perhaps set the camera's Y position in the Start method inside some sort of script that the camera object contains? Start is called on the frame when a script is enabled just before any of the Update methods are called the first time. \$\endgroup\$ Commented Dec 16, 2019 at 5:38
  • 2
    \$\begingroup\$ I realized now that it's probably better to use the Awake method for this case. For more information on their differences and their usage, see here: docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html \$\endgroup\$ Commented Dec 16, 2019 at 5:41
  • \$\begingroup\$ When should this one time setting occur? When the object first spawns? (If so, @Daniel_1985, you may want to post your comments as an Answer that can be accepted) Or do you want this setting to occur at some other time? \$\endgroup\$
    – DMGregory
    Commented Dec 16, 2019 at 23:42
  • \$\begingroup\$ @DMGregory To be honest, I'm not entirely sure when to set the setting but I guess the camera's position can be set to the other object's position as soon as the other object has spawned and its position has been successfully set. I'm not completely familiar with Unity so I don't wanna post an answer which doesn't even work. Just wanted to give OP a potential alternative to the other answer(s) posted. \$\endgroup\$ Commented Dec 17, 2019 at 4:47
  • \$\begingroup\$ @Daniel_1985 I am familiar with Unity and I think your answer is a good one. ;) Worth a try at least - you can always edit it if you get comments or votes suggesting it needs work. \$\endgroup\$
    – DMGregory
    Commented Dec 17, 2019 at 4:53

4 Answers 4

1
\$\begingroup\$

You can set the camera's Y position once by setting it in one of the built-in functions that's executed only once. In Unity there's two such functions and they're called Start and Awake.

Some information about the two functions:

  • The Start function gets called on the frame when a script is enabled, just before any Update function is called the first time.
  • The Start function is called only once during the lifetime of the script instance.
  • The Awake function gets called when a script instance is being loaded.
  • Just like the Start function, the Awake function is also only called once during the lifetime of the script instance.
  • The Awake function is always called before any Start functions.

Based on the above information, we can make sure that the other object, let's call it the target object, is fully initialized and correctly positioned before the position information is retrieved from it and the camera's position is set.

I'm not very familiar with Unity so the code example below is pseudo code and needs to be adapted/edited to work in Unity.

// Script attached to the camera object
public class CameraPositionScript : MonoBehaviour
{
    // The target object which the camera object should retrieve the position from
    private GameObject target;

    void Awake()
    {
        // The target object has been initialised at this point, so it's safe to reference it here
        target = GameObject.FindWithTag("TargetName");
    }

    void Start()
    {
        // Set the camera's Y position to the target object's Y position, this is done only once
        gameObject.transform.position.y = target.transform.position.y;
    }
}
\$\endgroup\$
0
\$\begingroup\$

If you have the cam target a Transform then you can use that so it just moves to whatever is in the selectedTarget field.

That way thing1 and thing2 ect could swap back and forth or whatever else you were selecting and all you would have to do is assign the transformTarget to a new transform.

To run only once in the update you can add a condition to check then you could do something like this.

public Transform selectedTarget;
string cachedTargetsName="";//set to cache the selections name
void Update()
{
   if(selectedTarget.gameObject.name != cachedTargetsName)
   MoveOnceToTargetsY();
}
void MoveOnceToTargetsY()
{        
     cam.transform.position = new Vector3(0, selectedTarget.position.y, 0);
     cachedTargetName = selectedTarget.gameObject.name;          
}

you can likely add a condition similar to if(selectedTarget.gameObject.name != cachedTargetsName)MoveOnceToTarget() various other ways to accomplish also like a OnChanged Event or something along those lines but this should get you on the right path.

\$\endgroup\$
0
\$\begingroup\$

If you want to this in the mid of your game, You can use "Flags". Syntax will be like below :

bool flag;  //It is False by default
fun()
{
   if (!flag)
   {
       flag = true; //set your flag
       //Your one time executable code
   }
 }

If you want to do it again, simply reset your "flag".

\$\endgroup\$
-1
\$\begingroup\$

void Update() { if (parameterIsMet) { cam.transform.position = new Vector3(0, thing.transform.position.y, 0); } }

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .