Search Unity

Question Moving 2D virtual camera with player input in 2D platformer

Discussion in 'Cinemachine' started by Trevor_trev, Jul 16, 2020.

  1. Trevor_trev

    Trevor_trev

    Joined:
    Jan 8, 2020
    Posts:
    11
    Hi! I'm trying to make a fangame of a 2D platformer. I'm currently trying to get the camera moving up and down with player input. As you can see from the video it appears (if I'm not mistaken, which I very well could be) that changing m_ScreenY simply moves the deadzone window instead of the camera.

    What should I be changing in order to move the entire camera? Am I going to have to temporarily disable or change the follow target when the input is pressed? If so, how would I go about doing that?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CameraScript : MonoBehaviour
    7. {
    8.     public CinemachineFramingTransposer framingTransposer;
    9.     public CinemachineVirtualCamera vcam;
    10.     public Playermovement pmov;
    11.     private void Awake()
    12.     {
    13.         vcam = GetComponent<CinemachineVirtualCamera>();
    14.         framingTransposer = vcam.GetCinemachineComponent<CinemachineFramingTransposer>();      
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (pmov.lookup)
    20.             framingTransposer.m_ScreenY += Time.deltaTime;
    21.  
    22.         if (pmov.lookDown)
    23.             framingTransposer.m_ScreenY -= Time.deltaTime;
    24.     }
    25. }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    If you set the Follow target of the vcam to the player, then the vcam will try to follow the player. If you want to move the camera independently of the player, then you are not following the player and should set up the vcam differently.

    There are several ways to go about this, and the best way will depend on what you're trying to achieve. Can you give a little description of how you want the camera to behave in response to your player movement and your user input?
     
    Last edited: Jul 20, 2020
  3. Trevor_trev

    Trevor_trev

    Joined:
    Jan 8, 2020
    Posts:
    11
    Yes I can!

    1. I want the camera to follow the player with specified deadzones. (I've been able to set this up, I'm just putting this down for specificity.

    2. The camera's default position should be set so the character is a little below the middle of the screen.

    3. Any time the character lands on the ground the camera should revert back to this default position.

    4. The player should be able to make the camera pan upward by holding the up button and downward by holding the down button. But when the character reaches the edge of the screen it should stop panning.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Thanks for that.

    What should happen if the user moves the camera down so that the player is at the top of the screen, then starts moving the player (player is on the ground). Does the camera stay down?

    What if the player jumps up from an initial position at the top of the screen? Does the camera follow, or is there a dead zone? If the camera follows, does it keep its position when the player lands (on the same or a different level), or does it always follow rule 3?

    Can you describe your dead zones? (maybe with a picture?) Are they always the same, or do they change depending on where the character is in the frame?

    Can the user also make the camera move horizontally until the player is at the edge of the screen, or is this only for vertical movement?
     
  5. Trevor_trev

    Trevor_trev

    Joined:
    Jan 8, 2020
    Posts:
    11
    The camera should stay down if the player moves it down and then starts moving. The camera should only reset on it's own if the character becomes airborne and then hits the ground.

    If the player jumps up from an initial position at the top of the screen the camera should follow the character as soon as the character's head hits the top edge of the screen, and when the character hits the ground the camera should revert back to it's default position, always following rule 3.

    This is only for vertical movement. The user cannot make the camera move horizontally unless they start walking, hit a dead zone, and continue walking in that same direction, also the camera should never reset to a default position on the x axis, only on the y axis

    The dead zones never change. Here's a picture of the dead zone positions and the inspector with the appropriate values if that also helps.

    Technically I do have a dead zone set up for the top of the screen but that's just because the yellow dot(well...square) is in the center of the character sprite. With the dead zone at this position the camera will move up when the character's head hits the top edge of the screen.

    Thank you for helping me out! It means the world to me! deadzone screenshot.png Inspector Screenshot.png
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Thanks for the details! I think I understand what you want to do.

    I have an idea. Create a second vcam, identical to the first, but set it up with no vertical dead zone - just with screen X/Y at the default position. Call it the Recenter Vcam. Make the horizontal dead zone the same as the first vcam. In both vcams, set the InheritPosition option to true:

    upload_2020-7-22_16-50-43.png

    Use the original vcam most of the time. However, when the player lands and rule 3 gets invoked, activate the Recenter Vcam, and let CM blend to it. After the blend time is finished, deactivate it, and CM will revert to the default vcam, which should be positioned to match the Recenter one, thanks to the InheritPosition option. Set up your CM Brain's Custom Blends so that it cuts back to the default vcam

    upload_2020-7-22_16-56-27.png

    For the Look Up/Down while the default vcam is active, don't touch the ScreenY. Instead, just modify the vcam's position.y in its transform. This should work so long as the player stays inside the dead zone. Pass that and the vcam's procedural positioning will push the vcam back to reframe.
     
  7. Trevor_trev

    Trevor_trev

    Joined:
    Jan 8, 2020
    Posts:
    11
    Awesome! I will try it first thing tomorrow morning. Thank you!