Search Unity

How could I recreate this camera?

Discussion in '2D' started by theDapperNurd, Oct 19, 2019.

  1. theDapperNurd

    theDapperNurd

    Joined:
    Nov 23, 2017
    Posts:
    2

    This person has their camera follow their player but also slightly in the direction of their mouse and I think it looks really smooth but I haven't been able to make it. I'm using cinemachine.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If you're asking if cinemachine has this built-in, I don't think so. There's probably a more proper way to create that behavior by making a new cinemachine extension subclass, but I still haven't gotten around to learning cinemachine's workflow for that.

    So my suggestion is to create a different follow target which is coded to always be at some position between the player and the mouse, based on the distance between them. Cinemachine should take care of following it with damped movement, etc.
     
    Last edited: Oct 20, 2019
    Antypodish likes this.
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You'd want to use Clamping for the camera. I have two videos to make today, and the third video will be this method of camera smooth follow. The script is designed, all I gotta do is get the videos up. My PC has been recording at 17 fps instead of 30fps causing lag issues. I'm working on getting this sorted out. However, I will be back here when I publish the video so you can see how this works. ;)

    Best part, the code I provide will work for side scrolling or top down as demonstrated in this video. There's only 73 lines of code (But I made comments you won't need so less than 70 actually.
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Here's the video I promised, had to cut the end due to severe lag. My computer sux, I guess :/

    Here's the script that makes it happen! Enjoy!

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollow : MonoBehaviour
    4. {
    5.     [Header("Player's Transform")]
    6.     [SerializeField]
    7.     private Transform player;
    8.  
    9.     [Header("Camera Offset Field")]
    10.     [SerializeField]
    11.     private bool maxXenabled = false;
    12.     [SerializeField]
    13.     private float maxXset = 0f;
    14.  
    15.     [SerializeField]
    16.     private bool minXenabled = false;
    17.     [SerializeField]
    18.     private float minXset = 0f;
    19.  
    20.     [SerializeField]
    21.     private bool maxYenabled = false;
    22.     [SerializeField]
    23.     private float maxYset = 0f;
    24.  
    25.     [SerializeField]
    26.     private bool minYenabled = false;
    27.     [SerializeField]
    28.     private float minYset;
    29.  
    30.     // Non inspector field object variables
    31.     private Vector3 camPos;
    32.     private Vector3 camVel = Vector3.zero; // Vector3(0,0,0);
    33.     private float camLagTimer = 0.25f;
    34.  
    35.     private void FixedUpdate()
    36.     {
    37.         camPos = player.position;
    38.         camPos.z = transform.position.z;
    39.  
    40.         // Horizontal clamping controls
    41.         if(minXenabled == true && maxXenabled == true)
    42.         {
    43.             camPos.x = Mathf.Clamp(player.position.x, minXset, maxXset);
    44.         }
    45.         else if(minXenabled == true)
    46.         {
    47.             camPos.x = Mathf.Clamp(player.position.x, minXset, player.position.x);
    48.         }
    49.         else if(maxXenabled == true)
    50.         {
    51.             camPos.x = Mathf.Clamp(player.position.x, player.position.x, maxXset);
    52.         }
    53.  
    54.         // Verticle clamping controls
    55.         if (minYenabled == true && maxYenabled == true)
    56.         {
    57.             camPos.y = Mathf.Clamp(player.position.y, minYset, maxYset);
    58.         }
    59.         else if (minYenabled == true)
    60.         {
    61.             camPos.y = Mathf.Clamp(player.position.y, minYset, player.position.y);
    62.         }
    63.         else if (maxYenabled == true)
    64.         {
    65.             camPos.y = Mathf.Clamp(player.position.y, player.position.y, maxYset);
    66.         }
    67.  
    68.         // Update the camera position
    69.         transform.position = Vector3.SmoothDamp(transform.position, camPos, ref camVel, camLagTimer);
    70.    }
    71. }
    72.