Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

CineMachine follow ahead in 2d

Discussion in 'Cinemachine' started by Paratrooper82, Feb 11, 2018.

  1. Paratrooper82

    Paratrooper82

    Joined:
    Jan 17, 2018
    Posts:
    13
    Hey guys, I just got CineMachine and I have a little issue I can't seem to figure out. I am currently building a 2d platformer and used the regular camera before, with a script I wrote. Basically I told the camera to follow the player, but at the same time follow ahead a little on the x axis when moving left or right (minus or plus on the X axis).

    Given it is a 2d platformer and when I move on the X axis I want the player to see ahead a little, I have come to this issue. CineMachine is awesome and I really enjoy that my camera doesn't move anymore every time I move the character left or right. But by using CineMachine I have also lost my "follow ahead" when moving left or right.

    Is there any way to have the camera smoothly follow ahead, depending which way I am going on the X axis?
     
    febryan-deolis likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    Have you tried Lookahead?

    upload_2018-2-12_12-16-52.png
     
  3. Paratrooper82

    Paratrooper82

    Joined:
    Jan 17, 2018
    Posts:
    13
    I tried that, it seems it does it for both Y and X though. Maybe I need to play around with it more. This is how I achieved it smoothly on the regular camera.

    Code (CSharp):
    1. void Update () {
    2.  
    3.         if (followTarget) {
    4.  
    5.             targetPosition = new Vector3 (target.transform.position.x, target.transform.position.y + 1, transform.position.z);
    6.  
    7.             //this moves the target of the camera ahead of the player
    8.             if (target.transform.localScale.x > 0f) {
    9.                 targetPosition = new Vector3 (targetPosition.x + followAhead, targetPosition.y, targetPosition.z);
    10.             } else {
    11.                 targetPosition = new Vector3 (targetPosition.x - followAhead, targetPosition.y, targetPosition.z);
    12.             }
    13.  
    14.          
    15.  
    16.             transform.position = Vector3.Lerp (transform.position, targetPosition, smoothing * Time.deltaTime);
    17.         }
    18.  
    19.     }
    On a side note. I have a flag that gets activated when you pass by, and serves as a respawn point, in case the player dies. The camera, after the player's death, moves to the nearest activated respawn point, and the player is respawned. Since using CineMachine, the camera seems to move in a strange way back to the player. Almost like CineMachine takes a second longer to center back on the player.

    I looked in the CineMachine script and saw that when the GameObject "Follow" is active, it'll follow the Player, but when the player dies he/she is basically "null" until respawn. It seems in that one second or frame, CineMachine doens't know what to do. Do you have any idea about that?
     
  4. Paratrooper82

    Paratrooper82

    Joined:
    Jan 17, 2018
    Posts:
    13


    I added a short video of the weird camera hangup.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    The upcoming version of CM has an "Ignore Y" option on the Lookahead. Should be available quite soon. If you need a solution sooner, you could make an invisible child of your target - with your custom script - and set the LookAt to that.

    It's not surprising that the camera stops moving when its target disappears. You could use an empty object as the target parent, and LookAt/Follow that. Make sure it never gets deactivated, and the camera will continue to follow it. Deactivating its child won't bother anyone.
     
    szzeee likes this.
  6. Paratrooper82

    Paratrooper82

    Joined:
    Jan 17, 2018
    Posts:
    13
    Hey Gregory, thank you for your help. So my fix right now is that I turned look ahead to 0. It doesn't give me the offset to look further left or right (x axis), but at least it fixes the weird jumping of the camera.

    I made a quick video to show what I was talking about.
     
    febryan-deolis likes this.
  7. sidcool3600

    sidcool3600

    Joined:
    Monday
    Posts:
    3


    As of 2019, I can confirm that this exists. However, on my screen when I open up cinemachine, the option simply isnt there? What do I do? Any help would be appreciated.

    upload_2019-12-2_21-45-48.png
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,134
    What version of Cinemachine do you have? It must be quite old.

    upload_2019-12-3_9-1-44.png
     
  9. sidcool3600

    sidcool3600

    Joined:
    Monday
    Posts:
    3

    Sorry to waste your time, I realized that I had BOTH The asset store version and the package version. All I did was delete everything and install the package one again; all my settings saved!
     
    Gregoryl likes this.