Search Unity

Best way to do Dark Souls- Style target Lock using Cinemachine?

Discussion in 'Cinemachine' started by Viggy1996, Jan 2, 2019.

  1. Viggy1996

    Viggy1996

    Joined:
    May 11, 2017
    Posts:
    39
    I am trying tio replicate the Dark souls like Target Lock in my game using Cinemachine. I actually coded my own targeting and it works well and good as it's quite simple as a Transform.LookAt , but I need the goodies of Cinemachine like Camera Collision Detection, which is much more complex.



    Is it a good idea to modify the camera manually when it is controlled by a Cinemachine brain or should I try to implement my targeting logic using Cinemachine. If so, I'm quite lost as how to look at a target while staying behind the player, essentially keeping both of them in frame.
     
    sd1515 likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    When the CinemachineBrain is controlling the camera, then it will override anything you try to do manually to it. The way to control the camera manually within the context of Cinemachine is to create a virtual camera with "Do Nothing" in Aim and Body (i.e. no procedural motion), and then control its transform and lens manually.

    For your other question, I would try using a FreeLook that follows the player but looks at a CinemachineTragetGroup that contains the player. Use the GroupComposer in Aim for all three rigs. When an enemy target enters the scene, add it to the target group. Then, the vcam will try to keep both the player and the enemy on screen. Remove the enemy from the target group when you're done with it.
     
    andreiagmu likes this.
  3. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    I'm answering to this thread instead of creating a new one since I'm having the same "issue".

    I'm using GetRig(int) to override the LookAt of the MiddleRig and using Group Composer base settings but things aren't looking good.

    Should I override all the LookAt and use Group Composer on all the rigs?
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    Yes, use the group composer in all the rigs. Set the FreeLook's main lookAt target to the group, so you don't need to override that in the rigs.
     
  5. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    This partially solved my problem, the only thing I need now is to lock the camera on the back of the player.
    What do you think can be the best way to achieve that?
     
    Last edited: Jan 10, 2019
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    Can you show an image of your FreeLook inspector, with the Body section expanded?
     
  7. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    Here it is. Thank you for your quick answers.

    Screenshot (226).png Screenshot (227).png Screenshot (228).png Screenshot (229).png
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    ok, you have it set to SimpleFollow. I assume you then want to rotate the player so that it's facing the same direction as the camera, thus putting the camera behind the player and not changing the camera aim, right?

    If so, then you can try adding this extension to your freeLook (drop it in the project, then select it from the "Extensions" dropdown in the vcam inspector).

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3. using Cinemachine.Utility;
    4.  
    5. /// <summary>
    6. /// Rotate the Follow Target to look along the camera forward axis
    7. /// </summary>
    8. [SaveDuringPlay] [AddComponentMenu("")] // Hide in menu
    9. public class RotateTargetToMatch : CinemachineExtension
    10. {
    11.     public bool m_YRotationOnly = true;
    12.     protected override void PostPipelineStageCallback(
    13.         CinemachineVirtualCameraBase vcam,
    14.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    15.     {
    16.         if (stage == CinemachineCore.Stage.Aim)
    17.         {
    18.             var follow = VirtualCamera.Follow;
    19.             if (follow != null)
    20.             {
    21.                 Vector3 fwd = state.RawOrientation * Vector3.forward;
    22.                 if (m_YRotationOnly)
    23.                     fwd = fwd.ProjectOntoPlane(state.ReferenceUp);
    24.                 follow.rotation = Quaternion.LookRotation(fwd, state.ReferenceUp);
    25.             }
    26.         }
    27.     }
    28. }
     
  9. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    Tha
    Thank you again but I think I didn't exaplain thinks in the right way. i want the camera the always face the back of the player withour using the mouse to rotate the camera which is not happening when I'm locking on a target even if I'm using Group Composer .

    I made a small video for this issue:
    https://drive.google.com/open?id=1dXGdn-W4jKfb24AfCRj_g3alhp5fnNTu

    The video is divided into: what happen -> what I want to achieve -> what happen
     
  10. Viggy1996

    Viggy1996

    Joined:
    May 11, 2017
    Posts:
    39
    I think what he wants is exactly how dark souls Lock On system works , in which the camera always stays behind the player,following him, but looks at target, in such a way that the player and target are both in the frame. Essentially
    * The camera is placed behind the player by an offset and stays there.
    * The camera must face the player's back at all times.
    * The camera must Look at a target from behind the player.
     
    Last edited: Jan 10, 2019
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    Not sure I understand 100% what you want.

    The main thing that's confusing me is that you're using a FreeLook. The FreeLook allows the camera to rotate around the player, using the mouse.

    If you want the player to always look away from the camera, then attach the extension I gave. That will force the player to always face away from the camera, by rotating the player to match, leaving full control of the camera to the user.

    So, one question. If you've "locked on" to a target, and the user rotates the camera to be in-between the player and the target, what is supposed to happen?
     
  12. Viggy1996

    Viggy1996

    Joined:
    May 11, 2017
    Posts:
    39
    There are two modes:
    * Free look : Player moves in the forward direction of the camera and the camera is free look. The user can control the camera with the Right Anlaog stick .
    *Locked On : Player is always between Camera and target . The Camera should look at the target while staying behind the player, so that both the player and the target are in the frame. The camera's forward direction is dependent on the player. The user cannot control the camera directly in this mode.

    Dark souls works like this. The video I linked in the Original Post is what it looks like in action.
     
  13. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    I think what's confusing you is the use of free look that, as you said, is meant to allow the user to rotate the camera using the mouse while we want the camera to stay focused on both target.
    For this issue I'm thinking about creating a virtual camera in a fixed position and toggle this camera when we're locking in a target.

    Screenshot (230).png
     
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    What happens in the second mode if the user turns the player to face away from the target?
     
  15. Viggy1996

    Viggy1996

    Joined:
    May 11, 2017
    Posts:
    39
    He cannot. The camera is Locked onto the target at all times. He has to leave the "Locked on" mode to control the camera manually . The right analog stick is useless in this mode.
     
  16. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    Never personally played Dark Souls but for my instance if you are running away from a target after you locked on it I just switch back to free look mode.

    EDIT: Or, yes, you can toggle off the lock mode.
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    ok, then this looks like a case of 2 vcams: a freeLook for normal mode, and a "locked" vcam for locked-to-target mode.

    The FreeLook mode seems to be a normal FreeLook on the Player, no target group.

    The Locked mode sounds like a simple vcam with Transposer and GroupComposer on a group containing player and target. Use the group as the LookAt target, and the player as the follow target. Use a Lock to target with World Up binding mode, and set the offset to something interesting. If you like, you can have a script that dynamically adjusts the offset (within a reasonable range) so that the user can vary the point of view a little.

    Enable/disable the appropriate vcams when you change mode, and CM will do a smooth blend between them. Make sure you enable the "inherit position" checkboxes on the vcams.
     
    andreiagmu and Viggy1996 like this.
  18. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    Happy to see that I was in the right direction but I probably missed something setting up the lock on camera.

    Screenshot (231).png
     
  19. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,501
    Hard to respond without knowing what you're seeing that's wrong.

    You probably need to check "inherit position" here and on the FreeLook also.

    Right now you've got the group composer set up to zoom/dolly in/out to maintain group screen size. Probably you don't want that. Disable that feature by setting the Framing mode to Dolly Only, and the Max dolly in/out to 0.
     
  20. Viggy1996

    Viggy1996

    Joined:
    May 11, 2017
    Posts:
    39
    Could you post the results here if you completed ? I would like to see how it turned out.
     
  21. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
  22. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    Making progress, missed the group thing. Will keep you posted.
     
  23. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
  24. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    Done! Just added a LookAt for the player when we're locked on a target.
    Working on polishing things up with cinemachine.
     
    Last edited: Jan 11, 2019
  25. Viggy1996

    Viggy1996

    Joined:
    May 11, 2017
    Posts:
    39
    Please do post the cinemachine component screenshot and the final result. I'm curious
     
  26. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    386
    Here it is! Note that is both about the code and the cinemachine component ;)
    Hope this can help you.

    Screenshot (235).png Screenshot (236).png
     
    andreiagmu and Viggy1996 like this.
  27. sleepyBear94

    sleepyBear94

    Joined:
    Oct 14, 2018
    Posts:
    1
    Hi I'm also at this point and I can't seem to fix it, may I know what you did to get the dark souls type lock on behavior?