Forum rules - please read before posting.

2.5D Camera settings

edited July 2020 in Technical Q&A

Hello! I watched making 2.5D game tutorial, and have some questions.
1. Is it possible to make the camera zoom in when player is further away?
2. Can you have the camera lock on to an empty object 1m in front of player instead of locking on to the player?
3. If the camera can zoom in and spin, can you keep its frustum within background image boundary?

Regards!

Comments

  • edited July 2020

    Welcome to the community, @dzft3w.

    AC's 2.5D mode works on the basis that the background is pre-rendered at a fixed position and angle, meaning it only works if the camera is locked in place.

    That's not to say AC can't work with fixed backgrounds and moving cameras - but it won't be using AC's specially-named 2.5D workflow.

    When creating a new scene, the Scene Manager gives you the option to override the game's perspective (i.e. 2D, 2.5D, 3D) for that scene - so you can make specific scenes in your game 3D while the rest are 2.5D.

    For such scenes, you'd need to have the background be a physical object in the scene - so that the camera can move/zoom independently from it.

    This tutorial covers how this principle, though it makes use of a 2D camera. If you want to be zooming/panning/spinning, you can make use of a standard GameCamera prefab. This camera supports zooming based on the player's position, and can also be constrained so that it can't move too far horizontally or vertically.

    By default, the GameCamera's Target will be set to the Player, but you can override this by unchecking Target is Player? in its Inspector, and then assigning your intended target in the field that then appears. If this object is a child of your Player object, just keep the Player in the scene file so that the reference isn't broken.

  • edited July 2020

    Hi,Chris. Thanks for answering.
    I have a different workflow. I managed to project my render image with depth channel to the camera so it can match with actual 3d object. This way from a fixed position camera perspective, the image can be background and foreground the same time. when applying a small camera shake or spin, the player can get to feel the depth of the image.
    https://ibb.co/BLyL0XP
    I want to add a bit more dynamic to camera perspective by unlock and narrowing FOV and have the camera spin and track player, but I don't want it spin out of image frame. I tried to edit the GameCamera.cs, after line 266 I tried clamping lookDir, but I find it hard to deal with quaternion, vector3 clamping, FOV change...Is there a way to make the camera tracking easier by constrain its current frustum within its original frustum?

  • I managed to project my render image with depth channel to the camera so it can match with actual 3d object.

    Interesting approach!

    Is there a way to make the camera tracking easier by constrain its current frustum within its original frustum?

    I believe you could do this by determining if reference objects placed either side are in view or not.

    Try this: create two empty GameObjects ("LeftEdge" and "RightEdge") and position them either side of the background image.

    Then attach the following to your GameCamera, and assign those two in its Inspector:

    using UnityEngine;
    using AC;
    
    public class DynamicFrustrum : MonoBehaviour
    {
    
        public Transform leftMarker, rightMarker;
        public float rotationFactor = 1f;
    
        private void LateUpdate ()
        {
            float offsetAmount = 0f;
            float pixelWidth = Camera.main.pixelWidth;
    
            Vector3 leftMarkerScreenPosition = Camera.main.WorldToScreenPoint (leftMarker.position);
            if (leftMarkerScreenPosition.x > 0f && leftMarkerScreenPosition.x < pixelWidth)
            {
                // Too far left
                offsetAmount = leftMarkerScreenPosition.x;
            }
            else
            {
                Vector3 rightMarkerScreenPosition = Camera.main.WorldToScreenPoint (rightMarker.position);
                if (rightMarkerScreenPosition.x < pixelWidth && rightMarkerScreenPosition.x > 0f)
                {
                    // Too far right
                    offsetAmount = rightMarkerScreenPosition.x - pixelWidth;
                }
            }
    
            Vector3 currentRotation = transform.eulerAngles;
            currentRotation.y += offsetAmount * rotationFactor * Time.deltaTime;
            transform.eulerAngles = currentRotation;
        }
    
    }
    

    This'll replace the GameCamera's Spin rotation "Constrain" option, so uncheck that. This works by updating the rotation in LateUpdate, i.e. after the GameCamera has updated itself.

    You may need to tweak the "Rotation Factor" value, but a quick test on my end seems to work.

  • The code was not good when rotation factor is set larger. It will struggle and start jittering, fighting at edge of frame. I did some researches and succeed with clamping lookdir vector.
    Could you help me with some new questions?
    1.I want to toggle on/off spin lock and FOV lock in play mode through code or action list.
    2.Change FOV constrain attributes in play mode.
    3.When in Direct control, press up key makes character run towards camera vanishing point. But I want to make it run straight up in screen vertical direction instead.

  • Toggle on/off spin lock and FOV lock in play mode through code or action list.

    Those are the lockYRotAxis and lockFOV variables respectively.

    Here's a script with functions to toggle these both on and off:

    using UnityEngine;
    
    public class ToggleCameraProperties : MonoBehaviour
    {
    
        private AC.GameCamera gameCamera;
    
        void Awake () { gameCamera = GetComponent <AC.GameCamera>(); }
    
        public void SpinLockOn () { gameCamera.lockYRotAxis = true; }
        public void SpinLockOff () { gameCamera.lockYRotAxis = false; }
    
        public void FOVLockOn () { gameCamera.lockFOV = true; }
        public void FOVLockOff () { gameCamera.lockFOV = true; }
    
    }
    

    Attach that as a component to your GameCamera, and you can then use the Object: Call event Action to reference this component and trigger either of the four functions to set these two properties on or off.

    Change FOV constrain attributes in play mode.

    This is set by the GameCamera's constrainFOV Vector2, where constrainFOV[0] is the minimum constraint, and constrainFOV[1] is the maximum.

    Though, if you want to change these properties to pre-set values, it may be easier to create a second camera with those values and switch to it with the Camera: Switch Action.

    When in Direct control, press up key makes character run towards camera vanishing point. But I want to make it run straight up in screen vertical direction instead.

    Do you mean the y-axis, or up in screen-space when the camera is at an angle?

    The game needs to be 2D for the former. For the latter, try checking Account for player's position on screen? in the Settings Manager.

  • What I meant by screen vertical was moving away from the camera but currently if player is not standing at the center, it will run in diagonal direction when pressing up or down like red arrow direction in photo. I want it follow the green arrow direction, which I think is more intuitive.
    https://ibb.co/stQZQTP

  • That is the intent behind the "Account for Player's position on-screen?" option - what effect did if have when enabling it?

  • That option did not work for me. It did not affect movement direction, but when player stops, the character will turn to face correct direction. Maybe somewhere went wrong. I was using unity third person controller and have integration scrips attatched.

  • edited July 2020

    Which integration scripts - this one?

    Temporarily add the 3D Demo game's Player, Tin Pot, into the scene to override your own. With the option checked, does he move more correctly?

    If so, the equivalent code to update the integration script should be to replace:

        m_CamForward = Vector3.Scale (Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
        m_Move = v*m_CamForward + h*Camera.main.transform.right;
    

    with:

        Vector3 forwardVector = (KickStarter.player.transform.position - KickStarter.CameraMain.transform.position).normalized;
        Vector3 rightVector = -Vector3.Cross (forwardVector, KickStarter.CameraMain.transform.up);
        m_Move = (v * forwardVector) + (h * rightVector);
    
  • Yes, that one.
    Tin Pot moves correctly.
    The replaced code fixed the problem. :)

  • Hi, Chris
    I set a trigger and a actionlist for player to press a key and switch camera, I got inconsistant result from input check button command. I guess the problem is I want to get key down, not get key pressing. Is there a command for it?
    https://ibb.co/McQ3ZJg

    Thank you!

  • Switch to an Active Input instead - it's much more reliable.

    See the Manual chapter of the same name, but Active Inputs allow you to run ActionLists upon an input button being pressed. They're available in the top toolbar under "Adventure Creator -> Editors -> Active Inputs".

    If you want to make such an input work when inside e.g. a Trigger, you can make it disabled by default - and then enable / disable it when entering / exiting the Trigger.

    You can do this with the Input: Toggle active Action - one on an "On Enter" Trigger to enable it, and another on an "On Exit" Trigger to disable it. You can attach multiple AC_Trigger components to the same object, so that they both work with the same collider boundary.

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.