Check if the Camera turns to the left or to the right from another Script

Hi there,

Im new in Unity and trying to do a Jump and Run Horror Game. The Camera is Orthographic, so i try to do a lot of Effects through Perspective.

My Problem, i want to make a Forest with Trees in the Background which are Static, but in the Foreground the trees should move slowly from left to right, or right to left demanding on the direction the player is moving. But sometimes the player is moving and the camera does not. So the moving trees look a little strange. Now i thought it would be the best to bind the trees on the movement of the camera, but i dont know how to make a script which says something like: If camera goes right, trees are going left. Can you please help me.

Long story short: How can i check from another script if my camera GameObject goes left or right?
Because it just has a Transform and Cinemachine as Components.

Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:

It isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

ALSO… if you’re needing camera work:

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

1 Like

Besides the good example of Kurt-Dekker, another approach I like is to work with events and a base EventManager.

The class that wants to know when the camera has moved registerers itself at the EventManager. The other class that is responsible for moving the camera, sents an event when it has moved the camera.

You can use this mechanism throughout your game for all kinds of things like rewards, coins earned, etc.
I like this implementation Unity3D C# Event Manager · GitHub

Thank you all for the Reply. But i solved my Problem now with:

On the Main Camera i have the Script:

public float currentXPosition
public bool left = false;
public bool right = false;

void Start()
{
currentXPosition = transform.position.x;
}

void Update()
{
if (transform.position.x > currentXPosition)
    {
        right = true;
        left = false;
        currentXPosition = transform.position.x;
    }
 else
    {  
        right = false;   
    }
if (transform.position.x < currentXPosition)
    {
        left = true;
        right = false;
        currentXPosition = transform.position.x; 
    }
    else
    {      
        left = false;
    }
    }
}

So when the Camera GameObject turns left or right the bool left or right is on.

And on the GameObject of the trees i have a Script that moves them to the left or to the right if the bools of the MainCamera are on or of.

I think its not what a real programmer would do. But its easy to unterstand for me and it works. :slight_smile:

But thank you again. Every time something works like i have imagined it, its like seeing your child taking the first steps. Im so proud. xD

1 Like

Great, sounds good. Just a small tip to simplify your code…

right = transform.position.x > currentXPosition;
left = transform.position.x < currentXPosition
currentXPosition = transform.position.x
1 Like