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 FreeLook with "external" look at target constraint for LockOn

Discussion in 'Cinemachine' started by Sangemdoko, Mar 16, 2019.

  1. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi,

    I am making a 3rd person action game with a free look camera. I am having difficulties with my lockOn system.

    I am trying to make a LockOn system similar to Nier Automata. For those unfamiliar with the game, it works as follows when locked on an enemy target:

    1) The camera always stays on the orbit rigs (top/mid/bot of the freelook)
    2) The camera rotates to face the enemy target (the further away the enemy is from the center of the screen the faster the camera rotates). When I say rotate I mean it moves on the orbit rig.
    3) The player can override the "rotation to face the enemy (on step 2)" when they move the mouse/joystick.

    The result is that the camera has the character always in the same place on the screen, while at the same time the "enemy target" is in the center of the screen, except if the player wants to look around him/her.

    I found this video, it shows the lockOn at minute 3:30 (not the best demonstration, but it gives you an idea).


    For my setup I have two FreeLook virtual cameras. The default one and the "lockon" one, they are very similar, the only difference is the orbits height/radius. When I lock on a target I switch the active vcam by changing the priority value of the lockon vcam.

    On lockon vcam I have a custom extension that computes where the camera should be facing and changes the X_axis and Y_axis inputAxisValue accordingly. It does not work well, it's clunky.

    So have two main problems.

    1) When I switch from default vcam to lockon vcam, the newly activated camera always starts on the bottom rig instead of inheriting the previous camera's position. In both vcams I have "Transition" -> "Inherit Position" checked.

    2) The lockon extension I made is clunky and jitters. Is there any better way to approach my problem? I am not very familiar with Cinemachine, so maybe I missed something. I am pretty sure that changing the inputAxisValue is not the correct way to do this, but its the only one I could think of.

    Thank you for your time.
     
    andreiagmu likes this.
  2. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    To give you a better idea of what I am trying to achieve here is my cinemachine vcam extension

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. namespace SleepingPenguinz.PhaeProject.CharacterAssist
    7. {
    8.     public class CinemachineFreeLookLookAtExtension : CinemachineExtension
    9.     {
    10.         public Transform lookAtTarget;
    11.         public CinemachineFreeLook freeLookVCam;
    12.         public Vector2 axisSpeed = new Vector2(2, 4);
    13.         public Vector2 axisThreshold = new Vector2(0.005f, 0.005f);
    14.         public Vector2 axisDamp = new Vector2(3, 2);
    15.  
    16.         protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    17.         {
    18.             if (freeLookVCam == null) { Debug.LogError("this extension only works on freelook vcams"); return; }
    19.             if (lookAtTarget == null) { return; }
    20.  
    21.             Vector3 wantedLookDirection = (lookAtTarget.position - transform.position).normalized;
    22.             Vector3 currentLookDirection = Camera.main.transform.forward;
    23.  
    24.             //Vector3 normal = Vector3.Cross(currentLookDirection, wantedLookDirection);
    25.             //float cosAngle = Vector3.Dot(currentLookDirection, wantedLookDirection);
    26.  
    27.             //var rotate = Quaternion.LookRotation(wantedLookDirection);
    28.  
    29.             //Debug.DrawRay(transform.position, currentLookDirection, Color.blue);
    30.             //Debug.DrawRay(transform.position, wantedLookDirection, Color.red);
    31.  
    32.             Vector3 LookDirX = Vector3.ProjectOnPlane(wantedLookDirection, Vector3.up);
    33.             //Debug.DrawRay(transform.position, LookDirX, Color.magenta);
    34.             float x_angle = Vector3.SignedAngle(currentLookDirection, LookDirX, Vector3.up);//model from Blender
    35.  
    36.             Vector3 LookDirY = Vector3.ProjectOnPlane(wantedLookDirection, Vector3.right);
    37.             //Debug.DrawRay(transform.position, LookDirY, Color.yellow);
    38.             float y_angle = Vector3.SignedAngle(currentLookDirection, LookDirY, Vector3.right);//model from Blender
    39.  
    40.  
    41.  
    42.             //Debug.DrawLine(transform.position, wantedLookDirection + transform.position,Color.blue,0.01f);
    43.             //Debug.DrawLine(transform.position, currentLookDirection + transform.position, Color.red, 0.01f);
    44.  
    45.             //float diffX = (currentLookDirection.x - wantedLookDirection.x) + (currentLookDirection.z - wantedLookDirection.z);
    46.             //float diffY = (currentLookDirection.y - wantedLookDirection.y);
    47.  
    48.  
    49.             //float horizontalMovement = diffX * diffX* diffX;
    50.             //float verticalMovement = diffY* diffY* diffY;
    51.             //Debug.Log(x_angle);
    52.             float x_angle_divided = x_angle / 180f; //0-1
    53.             float y_angle_divided = y_angle / 180f; //0-1
    54.  
    55.             float x_angle_damped = DampAbs(x_angle_divided, axisDamp.x);
    56.             float y_angle_damped = DampAbs(y_angle_divided, axisDamp.y);
    57.  
    58.             float x_angle_speedup = x_angle_damped * axisSpeed.x;
    59.             float y_angle_speedup = y_angle_damped * axisSpeed.y;
    60.  
    61.             float sqrDistanceToTarget = (lookAtTarget.position - transform.position).sqrMagnitude;
    62.  
    63.             float threshlodX = Mathf.Clamp(axisThreshold.x / (x_angle_divided *sqrDistanceToTarget), axisThreshold.x, 90);
    64.             float threshlodY = Mathf.Clamp(axisThreshold.y / (y_angle_divided * sqrDistanceToTarget), axisThreshold.y, 90);
    65.  
    66.             //Debug.LogFormat("clamp: {0}",threshlodX);
    67.             //Debug.LogFormat("speed: {0}", x_angle_speedup);
    68.  
    69.             float horizontalMovement = ThresholdAbs(x_angle_speedup, threshlodX);
    70.             float verticalMovement = ThresholdAbs(y_angle_speedup, threshlodY);
    71.  
    72.             freeLookVCam.m_XAxis.m_InputAxisValue += horizontalMovement;
    73.             freeLookVCam.m_YAxis.m_InputAxisValue += verticalMovement;
    74.  
    75.         }
    76.  
    77.         float ThresholdAbs(float a, float t)
    78.         {
    79.             if(Mathf.Abs(a) < t)
    80.             {
    81.                 return 0;
    82.             }
    83.             return a;
    84.         }
    85.  
    86.         float DampAbs(float a, float d)
    87.         {
    88.             float aAbs = Mathf.Clamp(Mathf.Abs(a), 0, 1);
    89.             float inv = 1 - aAbs;
    90.  
    91.             return a/(inv*inv*d);
    92.         }
    93.     }
    94.  
    95. }
    96.  
    Here is a video of what I currently get:
    https://drive.google.com/file/d/1UF96lbz8lQRmjmUZKwa0nplQ0pBUgnv4/preview

    As you can see I can move my character and the camera moves automatically to keep the "enemy" in view. I can still control the camera using the mouse to look around.

    I am close to the result I want but I still get some weird jitters now and then. I also have the transition problem where the camera I transition to starts on the bottom rig.

    Any advice is welcomed.
     
  3. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    If you could export a simple unitypackage with this setup and post it here (or PM me), I could play around with it a bit. It's difficult to diagnose in the dark.
     
  5. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi @Gregoryl , thank you for looking into my issue.

    I made a new project where I added the relevant scripts. I uploaded the project here. I use the latest cinemachine version 2.2.8.
    Please play around with the values on the LockOn virtual camera. I hope that what I am trying to achieve makes sense.
     

    Attached Files:

  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Your damping algorithm is causing the jitters.
    Replace it with this, it's much more stable and robust:
    Code (CSharp):
    1. float x_angle_damped = Cinemachine.Utility.Damper.Damp(x_angle_divided, axisDamp.x, deltaTime);
    2. float y_angle_damped = Cinemachine.Utility.Damper.Damp(y_angle_divided, axisDamp.y, deltaTime);
    For the Inherit Position to work properly the FreeLooks have to have the same Follow target. You'll need to rethink your approach in order to make that the case.

    Also, in your script, don't use Camera.main.transform, or vcam.transform, or transform. It can create nasty feedback loops and bad movement. Instead, get the vcam's desired position from state.RawPosition and state.RawOrientation.
     
    unity_334472229 and andreiagmu like this.
  7. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Thank you, that really helps out!
    I didn't know that the Follow target needed to be the same to inherit the position, I can fix that easily by changing some stuff around.

    I tried out the cinemachine damper and it's not giving me the results I expect, but I probably need to play with the camera movement speed, damp time and other parameters to get it right.

    I'll play around with it after work tomorrow. I'll post an update afterward since it might be useful for people who want to achieve a similar result.
     
    Gregoryl likes this.
  8. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi @Gregoryl I tried all the things you said but nothing worked for me.

    Using the same Follow target using inherit position on transition for both FreeLook camera. It still sets the Y value to 0 when I transition, this means my camera is always looking up (set to bottom rig) when I transition, instead of looking in the direction it left of.

    The damping does not help either with the jittering. I've tried many combinations of values and I still get jitters.

    I think my way of approaching this problem is wrong.

    Is there a way to somehow tell a Freelook camera to orient itself to copy the orientation of another camera?

    What I am trying to achieve is simply have the camera rotate around a pivot point and look at both the character (x center and slightly offset in the y axis) and the target (dead center of screen) at the same time. I still want to be able to influence the direction the camera is looking in, but if I'm not touching the mouse the camera should move to look at the target.

    Maybe I need to create my own camera type. Is there any documentation on how to write my own cinemachine camera type? Can we have access to the existing sourcecode?
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Cinemachine is shipped as source code, so you already have it.
    To make your own camera type, just copy one of the existing ones, rename it, put it into your assets, and modify it as you like.
     
  10. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi @Gregoryl Thank you,

    I changed my approach, I am now extremely close to the result I want. I am using a freelook and a virtual camera with framing transposer. I switch the camera to freelook using priority when the script detects camera movement input. Otherwise, it switches to the framing transposer camera.

    I have a single problem, the transition "inherit position" of the freelook.

    I need to inherit the actual unity camera position, not the other virtual camera position. The reason is that I can transition while the camera is transitioning. Currently, when that happens the camera view snaps to a position, instead of the virtual camera snapping to the camera view position.

    I tried to override the transition function in a class which inherits freelook. My problem is that I do not have access to some of the functions/attributes used in freelook, which are necessary for the transition to work properly.

    upload_2019-3-23_11-40-53.png

    I also tried copy pasting the entire freelook code in my assets. But some functions are not accessible due to protection levels. Some are internal, some simply can't be found.

    Is there any way you can change some of the methods/attributes protection level of the freelook code to protected or virtual?

    Or maybe you have a solution for me that would not require to change any of the protection levels? if you know a way to transition the position of a freelook to match the actual camera position, it would fix all my issues.

    I will send you a project package once I clean up some of my own code, so that it hopefully makes more sense.
     
  11. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    I've cleaned up my code, so it should make more sense now. Please find the project package attached

    I also made a diagram showing what I want
    upload_2019-3-23_16-20-18.png

    My LockOn script moves a pivot point in between the character and the target. My freeLook looks and follows the pivot. The LockOn camera looks at the target and follows the character using framing transposer to keep it in view.

    When I move the FreeLook camera with "Mouse X" and "Mouse Y" I switch from the LockOn camera to the FreeLook camera for a fraction of a second and then it switches back.

    Up to here, everything is good. The problem is when I Move the FreeLook Camera while the camera is transitioning from freeLook to LockOn. The freelook camera snaps to the lockOn camera position instead of snapping to the actual main camera position.

    You Can test it out in the demo scene. Select the cameras in the hierarchy during play mode. While moving the camera around using Mouse X and Mouse Y when locked on, look at the scene view, you will understand why the transition is causing problems. You can switch the Freelook camera inherit position toggle to see what happens if I do not inherit the camera position.

    As mentioned before I need to snap my freelook camera to the actual camera position when I transition to it. But I can't find a way to do it because I do not have enough access level to the freelook script functions.

    Thank you for your time, I really appreciate your help.
     

    Attached Files:

  12. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi,

    I wanted to share some of my improvements. I wanted to try the Cinemachine collider to see if it worked with my setup.
    Turns out it did not work the way I wanted with the LockOn Vcam. The reason being the cinemachineCollider tries to keep a line of sight with the LookAt target. Instead, I wanted to keep a line of sight with the Follow target.

    So I made my own CinemachineColliderFollow scripts by copy-pasting the CinemachineCollider script and changing a few lines of code. It works pretty well.

    I am very close to what I want to achieve. The only problem I wasn't able to fix is the transition problem. I do have one concern though. How can I keep the LockOn camera on the FreeLook camera rig? For now, I am manually choosing a distance from follow transform that is more or less the same as the freelook mid rig radius. But that might not be the best way to do this. I would be happy to hear any suggestions

    I made a video of Nier Automata to showcase how the camera works in that game.
    https://drive.google.com/file/d/1HJauQSGGWYH-sBTNooVw_yPP-lmj5j8_/view?usp=sharing

    I am uploading the latest version of the demo project.
    By playing the demo you'll see that I am very close to replicating the camera behavior. The major problem is the transition between LockOn vCam and FreeLook vCam while "Manual" LockOn is active.

    Once I add it in my actual game I'll be able to fine-tune the values and make sure the camera/character movement update in the correct order so that I do not get any jitter. I am using a character controller from the asset store so I am not 100% sure when the character movement updates.
     

    Attached Files:

  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The problem with the snapping is that when blends happen, the outgoing vcam is still feeding the blend until the blend is finished. In your case, you're interrupting a blend and then repositioning the outgoing vcam before the blend is done in order to recycle it as a new incoming vcam.

    I'm beginning to think CM needs a way to optionally disable the live feeding of a vcam into an outgoing blend, instead taking a snapshot of the position and using that. We can't have it behave like that all the time, because normally the default behaviour is correct. You just run into trouble when you recycle in-use vcams. Such a setting would be a little subtle, though, and it might just add to the confusion.

    In the meantime, you can work around this by not always recycling the same Lock-on vcam. Instead, maybe you could dynamically spawn them based on a template, or have a small pool of them that you cycle through. Check CinemahineCore.IsLive(vcam) to find out if it's safe to recycle. IsLive will return false if the vcam isn't participating in a blend.
     
  14. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi @Gregoryl

    I tried what you said but it did not work. Maybe I am doing it wrong.

    What I do is that I deactivate the LockOn vcam gameobject while I'm moving around the camera with the mouse.
    The problem is still the same, the freeLooks snaps to the vcam position instead of snapping to the Unity camera position.

    The LockOn vcam is always live in the blend when I need to transition back to freeLook. So I think the recycle idea cannot work.

    What I want is this

    Vcam freeLook/Brain ----------------------------> vcam LockOn (start) when moving the mouse around and stopping
    Vcam freeLook --------------Brain--------------> vcam LockOn (halfway) while not moving the mouse
    ----------------Vcam freeLook/Brain-------------- (snap to brain not vcam LockOn) when I move the mouse again before the brain reached vcam LockOn


    I'm wondering if I could replace the fromcam raw postition by camera.main.transform.postion? I know you said it is not good practice, but do you think it could work here?
    upload_2019-3-26_10-56-31.png
    I would just need to somehow get rid of the protection errors.

    Or maybe in the brain I can stop a transition before it completes?

    Thank you for the help
     
  15. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi @Gregoryl,

    After reading carefully what you said in your previous post multiple time I realized I misunderstood.

    I am now so very close to what I want!

    What I did is I added a third virtual camera which is just a transition camera. What I do is when I transition from LockOn to FreeLook I first move the transition camera to the brain raw position and orientation, and then once that quick transition is done I transition to FreeLook which inherits the transition camera position.

    It works! But... there is a transition glitch. I am not sure why. It is especially visible when I transition while all 3 cameras are close to each other. Its as if the camera zooms inside the character for a frame. I would really appreciate if you could have a quick look at my code and tell me if I am not doing it correctly.

    Thank you
     

    Attached Files:

  16. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi, sorry to bother you again,

    I've tried changing the logic of the transition a bit so that the transition camera is only used as a transition and not used by cinemachine brain. Otherwise, the flow of the camera movement is abruptly stopped while I would prefer a nice blend.

    Here is the new logic

    upload_2019-3-28_9-52-25.png

    I do not understand why the transition puts the freelook camera X and Y axis to 0. Is it because the transition camera has the same position/rotation as the brain? Should I be using a second "hidden" brain to do these unseen transitions?

    Thank you for the help
     

    Attached Files:

  17. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi, Sorry for spamming but I made some progress.

    I created a GetCloseY and GetCloseX axis functions but coping some part of codes here and there.

    I don't have the weird behavior where X and Y axis are 0 anymore. And it works! Kind of... I tried a few things but I can't get rid of the little hiccup while I transition while cinemachine brain in already blending. Instead of interrupting the blend I wish I could blend the blend into another camera position so that the movement could be smoother.

    The current thing I have is while blending from freeLook to LockOn, I want to blend back to freeLook.
    1) I copy freeLook pos in transition cam, I transition to transitionCam
    2) I copy cinemachine brain pos in freeLook, I transition back to LockOn
    3) I transition to freeLook.

    The reason I don't directly blend to freeLook is because cinemachine takes a percentage of the cams position to make the blend, If I snap freeLook while it is blending, the brain will snap.

    I hope that makes sense.

    If you have an idea to get mitigate the little hiccup that would be awesome. I feel like I am very close to the result I want. I won't be able to work on it until the weekend though, hopefully, you can give me some advice before then.
     

    Attached Files:

  18. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    220
    Hi again,

    Turns out I had some free time after work so I jumped back into trying some stuff out.

    It WORKS!

    I'm adding the project so people can have a look at the code and the camera settings.

    It works but I'm still unsatisfied with the results. When I move the character while moving the camera things get weird + my code is very power hungry so it's not fit for actual gameplay.

    I think the reason I get weird things when I move the camera and the character at the same time is because my 3rd transition camera does not move with the character...and I can't really do anything about it.

    ... so much time for this, it is sad... Well at least I learned a lot of stuff about cinemachine.

    I think I will simply have a lockOn where you cannot move the camera in my game. Other games do it this way and it shouldn't be too constraining on my gameplay.

    @Gregoryl Thank you for the advice I would still be trying to solve my problems in weird ways if it wasn't for your guidance.

    I saw in the package that there was a FreeLook experimental virtual camera. If you are working on a new implementation I would really appreciate if you could take into account a lockOn system for freeLooks. Or make an orbital framing transposer, which does not require user input. Or anything else that could make add constraints to orbital cameras.
    One other thing I think it would nice if there were constraints on camera angles compared to worldup. As in frame two targets but don't look down more than 45º. I'm not sure if that is even possible but it would be nice if it was.

    Anyways, Thank you

    Bye
     

    Attached Files:

    andreiagmu likes this.
  19. DeadlyArtist

    DeadlyArtist

    Joined:
    Oct 19, 2018
    Posts:
    9
    EDIT: Nvm, cinemachine created more problems than it solved. I still kept it, but I wrote custom movement and transition scripts. It was just too much of a hassle trying to tell cinemachine what to do when it does everything it can to ignore it.

    It took me around a full week writing proper logic, but now I'm way closer to the Rocket League camera than I could have ever been with Cinemachine logic, so I'm glad it was worth it.

    Original post:

    Hmm I think I'm in a similar predicament. Trying to get Rocket League like camera, which means lock to target with world up transposer on ground and simple world with follow up transposer in air. The camera smoothly transitions in rocket league, but not in cinemachine, because the simple world camera is still at its old position instead of the other camera's position, but in the end i found a way that solves it for me.

    The problem was that when externally changing the position, the camera would always snap inside the middle of 2 cameras, which would create hard, unpleasant camera switches in certain situations. So, I would only change the position if (CinemachineBrain.IsBlending == false)

    I searched long but only ended up with frustration, so this is all I have for now. Would be happy to see better or in built ways.

    Code (CSharp):
    1. // Checks whether target is grounded
    2. if (stateMachine.CurrentState == CharacterStates.IsGrounded.No)
    3.         {
    4.             if (!_entityCineMachineBrain.IsBlending)
    5.             {
    6.                 _entityCineMachineVirtualCamera2.ForceCameraPosition(_entityCineMachineVirtualCamera.transform.position, _entityCineMachineVirtualCamera.transform.rotation);
    7.             }
    8.  
    9.             _entityCineMachineVirtualCamera.Priority = 9;
    10.             _entityCineMachineVirtualCamera2.Priority = 10;
    11.         }
    12.         else
    13.         {
    14.             _entityCineMachineVirtualCamera.Priority = 10;
    15.             _entityCineMachineVirtualCamera2.Priority = 9;
    16.         }
    vcam1
    upload_2021-1-8_15-39-17.png

    vcam2
    upload_2021-1-8_15-39-49.png
     
    Last edited: Jan 29, 2021
  20. kitchen54

    kitchen54

    Joined:
    Nov 7, 2013
    Posts:
    6
    Cinemachine works great for my normal movement and I've set it up with my lockon which works pretty well except I've got some jittering with my character movement. Also, when I roll the camera stops following me and loses me in the shot. I've got its follow value set to a transform that does move with my rolling animation, and the look at is set to the locked on enemy. I really want to use Cinemachine but I cannot get it to stay with my character during this movement. It works fine with a quick backstep or attack animations.
     
  21. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Sounds like you might not have the right binding mode. You want one that ignores target rotations - like SimpleFollow or WorldSpace
     
  22. Mozq8

    Mozq8

    Joined:
    Dec 30, 2016
    Posts:
    61
    So to make the lock-on camera system like in Dark Souls I need to extend the cinemachine script? I thought that it was possible out of the box
     
  23. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @Mozq8 This is quite an old thread. You can do lock-on now out of the box with Framing transposer in Body, follow target = player and Hard Lock To Target in Aim, lookAt = lockOn target
     
    Mozq8 likes this.
  24. Mozq8

    Mozq8

    Joined:
    Dec 30, 2016
    Posts:
    61
    Thanks for the response, but I am having troubles with the setup, now its slightly tracking the enemy, but camera flies, orbits all over the way, where can I find "lookAt = lockOn target"?

     

    Attached Files:

  25. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You can't find it anywhere. I meant that the LookAt target should be the enemy that you're locking onto. Can you show a video of your result? The camera should be looking straight at the enemy, from a position such that the player is in-between.
     
    Mozq8 likes this.
  26. Mozq8

    Mozq8

    Joined:
    Dec 30, 2016
    Posts:
    61
    I've managed to tweak, it looks better now, but it jitters sometimes, I am using smart update and all my movement (rigibody) is in Fixed update via script, also how to deal when camera flies on Y axis?
     
  27. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    That's looking pretty good. I'm guessing that the camera is moving vertically beacuse you are looking at the target's feet. Instead, put a transform at eye level and look there (you could also use Composer instead of HardLoookAt, allowing you to tweak the screen position).

    For the jitter, I would guess that it's a clock mismatch. SmartUpdate will update the camera on Late or Fixed Update, depending on how the target moves. It detects movement by comparing the transform values. If your target's transform changes on both the physics and render clocks, there will be no unambiguous correct choice, so the vcam will be arbitrarily updated on LateUpdate, which seems to be what's happening:

    upload_2021-5-13_13-0-5.png

    You need to decide whether your characters move on Fixed or regular update, and stick to it, otherwise you will be plagued with camera jitter.

    What happens if you switch the Brain over to FixedUpdate?
     
    Mozq8 likes this.
  28. Mozq8

    Mozq8

    Joined:
    Dec 30, 2016
    Posts:
    61
    I see, when I change the brain to fixed update the jitter increased.

     
  29. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    ok. Do you have RigidBody Interpolation on for the character? If so, that's good because it converts the physics-generated movement to LateUpdate-compatible values. Leave the Brain on Smart or Late update.

    We will have to drill into your scene to figure out where the jitter is coming from. It may still have to do with how the character is being animated. Also: do you get jitter if you switch the vcam Aim to Composer?
     
    Mozq8 likes this.
  30. Mozq8

    Mozq8

    Joined:
    Dec 30, 2016
    Posts:
    61
    Yes, rigibody's interpolation is on, for some reason, I changed it back to smart update and Aim - Composer and the jitter is gone, so thanks a lot, Gregoryl!
    One thing though, when I am close to the enemy the camera is trying to orbit around the enemy, I suppose I need to tweak some parameters in Aim section?
     
  31. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You should choose your target point carefully, because it defines the camera angle, which in turn influences the position. Try keeping all targets at eye level, unless you want the camera to move vertically.
     
    Mozq8 likes this.