Search Unity

Stop following target and move manually

Discussion in 'Cinemachine' started by mrCharli3, Mar 10, 2018.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I want my Virtual Camera (2D) to stop following it's target and be controlled using my mouse. I already have a script for it that worked with the standard Unity Camera. How can I make my Cinemachine camera temporarily ignore it's follow target and move according to input? Via Script.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    The best way to do that is to use the #1 rule of Cinemachine: don't try to do it all with one vcam.

    Have a second vcam, controlled by the mouse, and activate it when you want it to take over. The Unity Camera will blend to it, and back again when you deactivate it.

    For your camera script, you can easily replace the Camera in it with a vcam (with Do Nothing in both Aim and Body), and it will work as before.
     
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Thank you. And if I want to control it with WASD? Forgot to mention that is an option too (in 2D, so not sure about FreeLook Camera). Current code looks like:

    Code (CSharp):
    1.  
    2. //Update...
    3. if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f) //move
    4.             {
    5.                 _placementDirection.x = Input.GetAxisRaw("Horizontal");
    6.             }
    7.  
    8.             if (Mathf.Abs(Input.GetAxisRaw("Vertical")) > 0.5f) //move
    9.             {
    10.                 _placementDirection.y = Input.GetAxisRaw("Vertical");
    11.             }
    12.  
    13.  
    14. void FixedUpdate()
    15.     {
    16.         _cam.transform.position += _placementDirection.normalized * _placementMoveSpeed * Time.deltaTime;
    17.     }
     
    Last edited: Mar 10, 2018
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Not a problem. _cam needs to be a CinemachineVirtualCamera, set up as I said with no procedural positioning. In that mode you can control its transform. You can still add handheld noise to it, if you want (in the Noise section of the vcam). That will not affect your script.
     
    mrCharli3 likes this.
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    Code looks ok, but context is everything, and I would need to see more of it.
    What does your object hierarchy look like?
    Where does this script live?
    Where is the Unity Camera?
    Where are the vcams?
    Pictures of inspectors and hierarchy would be helpful.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,719
    vcams should never be children of the main camera. That is very likely the source of the problem.
    Think of vcams as puppeteers of the camera. They are independent entities moving around the world according to their own rules, and they tell the Camera where to go.
    Put the main camera and all the vcams as siblings, ideally in the root, but you can make a container object for neatness if you want.
     
  7. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Was gonna edit my last post but accidentally deleted it... Anyways, here is a picture:



    The error I get is easy to explain, using this code inside my CameraManager (attached to my Main Camera) I try to move my vCam:

    Code (CSharp):
    1. //FixedUpdate
    2. if (placementCamMoving)
    3.             {
    4.                 Vector3 speed = _placementDirection.normalized * _placementMoveSpeed * Time.deltaTime;
    5.                 _vCamPlacement.transform.position = new Vector3(_vCamPlacement.transform.position.x + speed.x, _vCamPlacement.transform.position.y + speed.y, _vCamPlacement.transform.position.z);
    6.             }
    The effect I get:
    • The vCams transform moves according to the speed, and only when I press WASD, like normal movement.
    • The Main Camera transform moves, however the speed seems to be ADDED each frame, so even if I'm not holding WASD it moves with the same speed as the last frame. Any any motion gets added/subsracted, and not set.

    EDIT: Part of the problem is that the camera with no body has no distance set, so z = 0. which will not work in 2D.

    EDIT 2: Solved, I think part of issue like you said was the parenting, thank you!
     
    Last edited: Mar 10, 2018