Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to move camera when player is following while using framing transposer ?

Discussion in 'Cinemachine' started by imambabba, Apr 29, 2023.

  1. imambabba

    imambabba

    Joined:
    Feb 4, 2022
    Posts:
    5
    I'm working on a game that is similar to Icy Tower. In this game, the camera is always moving upwards. When the player jumps high, the camera needs to catch up to the player's new height. I'm using dead zones and soft zones to achieve this effect.

    However, I'm having trouble implementing the camera movement. I want the camera to move up along the Y-axis, but I'm unable to move it when it's following the player.

    I also tried using the OnTargetObjectWarped event in the CinemachineFramingTransposer component to move the camera up along the Y-axis, but this solution isn't working as expected. It was laggy and doesnt work properly.

    I'm looking for help on how to implement the camera movement along the Y-axis when the player jumps, while still using the CinemachineFramingTransposer component for the dead zone and soft zone effects. Any help would be greatly appreciated.
     
  2. antoinecharton

    antoinecharton

    Unity Technologies

    Joined:
    Jul 22, 2020
    Posts:
    189
    Heyyo :) ,
    Have you looked at dolly tracks? Would be a great way to constrain your camera.

    Edit: Dolly might be a little overkill for that. Maybe just adding a constraint on the Y axis would work?
     
    Last edited: May 1, 2023
  3. imambabba

    imambabba

    Joined:
    Feb 4, 2022
    Posts:
    5
    Thanks for sharing your solution, but I decided to use a regular camera instead. I managed to get camera controls similar to Icy Tower. Here's a simpler explanation of how I did it, in case someone wants to do something similar:

    1. Make an empty GameObject named "To Follow" and attach it to the Main Camera.
    2. Create a script called "CameraMoveUp" and attach it to the Main Camera.
    3. Lerp (smoothly transition) the Main Camera position to the "To Follow" GameObject position.
    Here's the code for the "CameraMoveUp" script:


    Code (CSharp):
    1.  
    2. public GameObject targetPosition;
    3. public float moveSpeed = 1f;
    4. public float timeMultiplier = 1f;
    5.  
    6. void Update()
    7. {
    8.     transform.position = Vector3.Lerp(transform.position, targetPosition.transform.position, moveSpeed * timeMultiplier * Time.deltaTime);
    9. }
    10.  

    1. Create a GameObject named "PlayerDetect" and attach it to the Main Camera.
    2. Add a BoxCollider2D to "PlayerDetect" and adjust its size and position for player detection.
    3. Make a script for player detection. When the player collides with the box, smoothly transition the camera speed to the player's Y velocity. Do the same when the player exits the box.

      Here is how it looks
    Here's the code for the player detection script:


    Code (CSharp):
    1.  
    2. CameraMoveUp moveUp;
    3. [SerializeField] Rigidbody2D rb;
    4.  
    5. private void Start()
    6. {
    7.     moveUp = FindObjectOfType<CameraMoveUp>();
    8. }
    9.  
    10. public void OnTriggerEnter2D(Collider2D collision)
    11. {
    12.     if (collision.tag == "Player")
    13.     {
    14.         moveUp.moveSpeed = Mathf.Lerp(moveUp.moveSpeed, Mathf.Abs(rb.velocity.y), 1.5f * Time.deltaTime);
    15.     }
    16. }
    17.  
    18. private void OnTriggerExit2D(Collider2D collision)
    19. {
    20.     if (collision.tag == "Player")
    21.     {
    22.         moveUp.moveSpeed = .3f;
    23.     }
    24. }
    25.