Forum rules - please read before posting.

Camera size follows player position

I have a corridor in my (2D) game that the player walks away from the camera, I want the camera to zoom in as he walks away (and zoom out as he walks towards, is there a way to get the camera to change size as he moves up and down the sorting map? I have set up a couple of triggers that change to a different zoomed in camera as he enters the trigger, and another to set it back when he enters that one, it's ok, but not exactly what I wanted, a one to one live change would be better if that's possible?

Comments

  • This is for a "GameCamera2D" camera type?

    Try this script (ScaleCameraWithTargetY.cs) attached to the camera, and fill in its Inspector:

    using UnityEngine;
    using AC;
    
    public class ScaleCameraWithTargetY : MonoBehaviour
    {
    
        public float targetMinY, targetMaxY;
        public float minOrthographicSize, maxOrthographicSize;
        public GameCamera2D camera;
    
        private void Update ()
        {
            float targetY = camera.Target.position.y;
            float yLerp = (targetY - targetMinY) / (targetMaxY - targetMinY);
            float orthographicSize = Mathf.Lerp (minOrthographicSize, maxOrthographicSize, yLerp);
            camera.Camera.orthographicSize = orthographicSize;
        }
    
    }
    
  • Thanks so much Chris, but it's saying to fix compiler errors when I try to add it to the camera :/ and the console error says this: Assets\Rose Cottage\Scripts\ScaleCameraWithTargetY.cs(13,26): error CS1061: 'GameCamera2D' does not contain a definition for 'Target' and no accessible extension method 'Target' accepting a first argument of type 'GameCamera2D' could be found (are you missing a using directive or an assembly reference?)

    I don't know what any of that means :dizzy:

  • oh and this too: Assets\Rose Cottage\Scripts\ScaleCameraWithTargetY.cs(16,10): error CS1061: 'GameCamera2D' does not contain a definition for 'Camera' and no accessible extension method 'Camera' accepting a first argument of type 'GameCamera2D' could be found (are you missing a using directive or an assembly reference?)

  • What's your AC version? The script should compile with the latest release.

  • Ah I am not up to date with AC, I will try that thank you Chris

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.