Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

[2D] Overshooting camera and back to max position

Discussion in 'Cinemachine' started by ktest112233, Aug 1, 2019.

  1. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    Hi,

    I am making a 2D platformer game and using the following camera extension to restrict the player's movement beyond the level edges.

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. [ExecuteInEditMode] [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    5. public class CinemachineLockAxis2D : CinemachineExtension
    6. {
    7.  
    8.     private float maxWidth;
    9.     private float maxHeight;
    10.  
    11.     public void SetMaxBounds(float width, float height)
    12.     {
    13.         maxWidth = width;
    14.         maxHeight = height;
    15.     }
    16.     protected override void PostPipelineStageCallback(
    17.         CinemachineVirtualCameraBase vcam,
    18.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    19.     {
    20.         if (enabled && stage == CinemachineCore.Stage.Body)
    21.         {
    22.             Vector3 pos = state.RawPosition;
    23.             pos.x = Mathf.Clamp(pos.x, 0f, maxWidth);
    24.             pos.y = Mathf.Clamp(pos.y, 0f, maxHeight);
    25.             state.RawPosition = pos;
    26.         }
    27.     }
    28. }
    Right now, the camera follows the player but stops abruptly when it reaches the edge.

    My question, is it possible to make it stop smoothly, overshoot a little and then move it back to the maximum position?
    Just like when scrolling a list view on mobile devices... it overshoots and then comes back to the maximum position.


    Any leads would be great. Thank you
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    5,551
    Why don't you use the CinemachineConfiner extension, and give it some damping?
     
  3. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    The camera movement is very odd if I lock X or Y axis... even though the bounds are greater than the camera size

    If I set pos.x or pos.y to zero... the camera still moves.. I guess its because the confiner is modifying the value?

    Code (CSharp):
    1.         if (enabled && stage == CinemachineCore.Stage.Body)
    2.         {
    3.             Vector3 pos = state.RawPosition;
    4.             pos.x = lockX ? 0 : pos.x;
    5.             pos.y = lockY ? 0 : pos.y;
    6.             state.RawPosition = pos;
    7.         }
    I used this script which you posted here.
    https://forum.unity.com/threads/lock-an-axis-in-2d-vcam-follow-mode.516370/
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    5,551
    Well, then perhaps you can get inspired by the code in Confiner, and add damping in that manner.
     
    ktest112233 likes this.
  5. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    Yup seems like it :)
     
unityunity