Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    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?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    1,298
    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:
    1,298
    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.