Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cinemachine look ahead with mouse

Discussion in 'Cinemachine' started by Gato800, Jan 19, 2019.

  1. Gato800

    Gato800

    Joined:
    Sep 12, 2018
    Posts:
    1
    I'm currently working on a twin stick shooter, and I want camera behavior similar to enter the gungeon's, where you can adjust the camera's position with the mouse, but always keep the player on screen. I'm wondering how I could achieve this effect with cinemachine2D. I know there's a lookahead feature in cinemachine, but I'm unsure how to manipulate this with anything other than the follow object. My only idea would be to create two virtual camera's and have one follow the player, and the other follow an empty with the mouse position, while somehow having the camera stay in between the two. I don't know if this is right, and if it is, i don't know how to do it. Any insight would help a lot!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Can't you create a vector between the mouse cursor and the player, and then focus Cinemachine on the midpoint of the vector? And then you could use the "between" vector to ensure that the player and the cursor isn't too far apart.
     
  3. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    41
    So I had this same question for a 2D game and this is how I implemented it (for anyone else who wants to know how to do this). Like what PGJ said, you find the midpoint (or quarter-point if you will) between your unit and mouse. Since Cinemachine requires a transform, you will probably need an empty GameObject for the Cinemachine to follow. As a result, set the empty GameObject's transform to the position that you want, and simply follow that GameObject instead of your unit. Here are some images of the code and end result.
     

    Attached Files:

  4. fomglib

    fomglib

    Joined:
    Aug 26, 2019
    Posts:
    1
    I made a formula (on line 10):

    Code (CSharp):
    1. public class CameraTarget : MonoBehaviour
    2. {
    3.         [SerializeField] private Transform playerTransform;
    4.         [SerializeField] private Camera mainCamera;
    5.         [Range(2, 100)] [SerializeField] private float cameraTargetDivider;
    6.  
    7.         private void Update()
    8.         {
    9.             var mousePosition = mainCamera.ScreenToWorldPoint(Input.mousePosition);
    10.             var cameraTargetPosition = (mousePosition + (cameraTargetDivider - 1) * playerTransform.position) / cameraTargetDivider;
    11.             transform.position = cameraTargetPosition;
    12.         }
    13. }
     
    YohanJabin likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,533
    I'll move this to the Cinemachine forum.
     
  6. bwaite87

    bwaite87

    Joined:
    Nov 23, 2017
    Posts:
    17
    thank you, I couldn't even get chatGPT to figure that out lol!