Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    7,658
    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:
    7,658
    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:
    Dec 2, 2019
    Posts:
    4


    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:
    7,658
    What version of Cinemachine do you have? It must be quite old.

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

    sidcool3600

    Joined:
    Dec 2, 2019
    Posts:
    4

    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!
     
    Adam_Myhill and Gregoryl like this.
  10. Wiechering

    Wiechering

    Joined:
    Jun 22, 2015
    Posts:
    9
    Can someone from the Unity Folks tell me if it is planned to have different values for Lookahead Time and Lookahead Smoothing for different axis?
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Not currently on our roadmap.

    You can implement it by having an invisible game object that tracks your target with the lookahead algorithm of your choice, and have the camera track that.
     
  12. Wiechering

    Wiechering

    Joined:
    Jun 22, 2015
    Posts:
    9
    Very good idea. Thank you!
     
    Gregoryl likes this.
  13. zedix28

    zedix28

    Joined:
    Jul 5, 2020
    Posts:
    4
    I did that, but it didn't work. (unity version: 2020.1.0f1)

    edit: it works on the y, but not on the x
     
    Last edited: Aug 7, 2020
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Can you give more details?
     
  15. zedix28

    zedix28

    Joined:
    Jul 5, 2020
    Posts:
    4
    I followed a tutorial by Brackeys and used the same values he did, but the camera looks above the player when he jumps and under him when he is falling, but it doesn't look in front of him when he moves on the x axis.
    Brackeys tutorial:
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    If you used the same values, you should get the same result. Can you post an image of your vcam inspector, and of your scene hierarchy, showing the vcam, the brain and the player?
     
  17. davidkooistra3

    davidkooistra3

    Joined:
    Jun 30, 2022
    Posts:
    2
    @Gregoryl I'm also experiencing the issue where the look ahead is not matching the Brackeys tutorial above, and also does not seem to be working properly on my own projects. The look tracker point is not going much ahead of the player even at look ahead time equal to 1. Any ideas for what is going on here?

    Here is a video screen capture I have made showing the issue. The video includes my settings for the cinemachine and brain:


    This is the project repo associated with the tutorial: https://github.com/Brackeys/2D-Camera

    I was using cinemachine version 2.8.6 and noticed the problem. After upgrading to version 2.9.5, the issue still remains.

    Thanks in advance for the help on this.
     
  18. davidkooistra3

    davidkooistra3

    Joined:
    Jun 30, 2022
    Posts:
    2
    In CinemachineBrain I changed the "Update Method" from smart update to fixed update, and that seems to have helped. Does that seem like the correct solution here? Maybe Fixed Update should be the default for a 2d cinemachine instead of smart update.
     

    Attached Files:

  19. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,079
    Yes, if you're using lookahead, I've found using FixedUpdate essential to stability and to prevent all manner of oddities.
     
  20. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    I've just downloaded the Brackeys project. Running it in 2022.2.5f1 with CM 2.9.5, it works as advertised. The effect seems to match that in the video. I don't know what you've done differently in your project.

    FixedUpdate vs SmartUpdate should make no difference whatsoever. If it does, it would tend to indicate an issue with your setup. Something about the way you're moving your player, I would guess. If you'd like to send me a small test project by DM I could take a look at it.

    @Unifikation is wrong about this. It's almost always preferable to use LateUpdate for cameras rather than FixedUpdate.
     
  21. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,079
    Maybe for you, but not for me... when using lookAhead, LateUpdate would be nice, but it's too sloppy for anything faster than an early 90's platformer.
     
  22. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You can certainly set up your game in such a way that that statement is true, but it's also possible to set up a game in such a way that it's not. If you use FixedUpdate for cameras, you can gat aliasing which can result in unsmooth movement.

    But let's not hijack David's thread with this discussion :)
     
  23. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,079
    I think you're going to have to get into it, somewhat, as FixedUpdate works for him, and me...

    So what's the magic that makes it work best with LateUpdate and isn't sloppy?

    Neither of us seem to have had. problems with aliasing. What causes that when using FixedUpdate, and how is it/could it be that we've seemingly inadvertently avoided this?
     
  24. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,079
    Some wisdom from the sage himself:

     
  25. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    It's generally better to enable RigidBody interpolation when you can. In cases where you can't do that for whatever reason then you can fall back to FixedUpdate, but it's a compromise solution.