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 looks in the "wrong" direction

Discussion in 'Cinemachine' started by eliasjgnilsson, Dec 15, 2021.

  1. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Sorry for second question in a short amount of time, it is very hard to find information on some subjects regarding cinemachine camera.

    Background
    I am having the camera following and look at a ball. It follows it nicely when the ball is shot and I can look around with the mouse in the meantime as expected.
    I have a function where the ball is reset to its previous position which basically just moves the transform to the position from where the last shot was made.

    Problem
    When the ball is reset the camera seems to chose a completely random direction to look in. More often than not (fascinating often acctually) it is looking in almost the opposit direction to where it should.

    Desired result
    Illustrated in the very high quality image below you can see the ball when it is at position 1 travelling towards the black hole. Now when I reset the ball to the red position 2 I want the camera to look at the ball and hole (the ball being between the camera and the hole if that makes sense).
    Namnlös.png
    What I tried
    When resetting ball posiiton I tried to first move the ball to position 2, then change cinemachine lookAt to hole transform, then back to ball transform. This however made no noticable difference than before.

    Main question
    How can I through code set in what direction the camera should look in?
     
  2. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    I think this has to do with that the camera follows the ball back, hence it will basically turn around and go to the ball and then facing the wrong direction.

    I think this is related to CinemachineOrbitalTransposer.Heading, but I can't figure out how to change it. I could always save the position/rotation of the camera before the shot is made, but then I need to change the Heading somehow to be in the right direction.
     
  3. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    I am not sure, if I understand your question. You can use a TargetGroup to frame more targets at the same time.
     
  4. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    No no, that was a bad explanation from me.

    I always want the camera to be behind the ball, rotated so that the hole is in front. See here:

    whatIWant.png

    Right now when I shoot the camera follows the ball. Then when I reset the ball by immidately moving its transform position back to where I shot, the camera seems to pick the side closest on the ball and look at the ball.
    If you look in the picture below, the ball and camera starts in pos 1 with the camera being behind the ball, looking at the ball in the holes direction. Then I shoot the ball and camera follows. At pos 2 I reset the ball position. Then it seems like the camera is picking the closest direction back to the ball, hence looking in the other way than before.

    Namnlös.png

    I hope the issue is more clear now :)
     
    gaborkb likes this.
  5. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Use Transposer with Lock to Target With World Up.

    If your ball is rolling, then the camera will move around, because it depends on the forward vector of the ball to position itself. The Follow target's forward vector should be the balls velocity vector (normalized).
     
  6. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Yes the ball is rolling. I am sorry,you will have to be way more basic than this with me when it comes to Cinemachine. Transposer? Lock to Target? World up?
     
  7. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Sorry :)

    You can create a virtual camera by right clicking in the Hierarchy -> Cinemachine -> Virtual Camera. Set your Target to in the Follow and Look at fields. Then in the Body part, set Transposer.
    Something like this:
    Screen Shot 2021-12-16 at 10.28.41 AM.png
    The Body components control the position of the camera, and the Aim components control the rotation of the camera.

    When you reset your ball, you'll probably need to call OnTargetObjectWarped. You can access this function from your virtual camera.

    In general a good introduction to cinemachine is our example scenes.
    Samples.png
     
  8. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    I already have a free look camera, it works perfectly (except this small thing). I set target and lookAt during runtime through code since I instantiate the balls when scene is loaded. Can the problem be solved with the free look camera or do I need to do a (probably very tedious) switch to a normal virtual camera?
     
  9. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    It does not sound like that Freelook camera is for a game like this. Are you using your mouth to control the camera?

    It should be very simple. Why is it tedious?
     
  10. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Yes you use the mouse to aim around, it was the reccomended choice for 3rd person shooter game and basically I want the same behaviour.

    The game is big with lots of dependencies. But I will give it a try
     
  11. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Just a virtual camera did nothing really.
    I mean the free look camera is so close of working, except this minor issue. Isn't there any way of telling the free moving camera where to look, or from what angle to look at the ball?
     
  12. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    You can create two Freelooks, one that looks at the hole and one that looks at the ball. Instead of changing the LookAt targets, switch between the vcams by enabling/disabling them. This way the ball Freelook will keep its state as it was before the switch.

    If for some reason, you don't like that you can try
    Code (CSharp):
    1. vcam.ForceCameraPosition();
    to force freelook behind the ball.

    Try this script:
    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. public class ForceBehindBall : MonoBehaviour
    5. {
    6.     public CinemachineVirtualCameraBase vcam;
    7.     public Transform ball;
    8.    
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.Space))
    12.         {
    13.             vcam.ForceCameraPosition(ball.transform.position - ball.transform.forward, Quaternion.identity);
    14.         }
    15.     }
    16. }
    Ok, in that case, check out these example scenes: 3rdPersonWithAimMode, AimingRig.
    Check this video:


    Where?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    It looks to me as though your FreeLook has its BindingMode set to SimpleFollowWithWorld Up. Have you tried changing the BindingMode to WorldSpace?

    upload_2021-12-21_15-3-40.png
     
  14. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Yes, it doesn't really make a difference.