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

Initial viewing direction of POV camera

Discussion in 'Cinemachine' started by AntLewis, Jan 29, 2019.

  1. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Hi there, I have a virtual camera setup so the Aim is using POV. Is there anyway programmatically that I can set the initial direction the POV camera is looking at as it blends from the previous camera to this one (before moving according to player input)?

    Many thanks for any asistance.
    Cheers
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Just set m_HorizontalAxis.Value and m_VerticalAxis.Value. The amounts represent offsets in degrees from the vcam's z axis
     
  3. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Thanks for such a prompt reply on this. To explain, I'm transitioning from a standard follow camera to a POV camera and at the end of the transition I want the POV camera to look at a gameobject. This nearly works, but the targetting it slightly off. I think the issue is, to accurately target the gameobject, I need to get the (final) position of my POV camera after it's transitioned from the followcam. Is there anyway to access the final position of the POV camera before the transition has started? Also, if I'm talking gibberish please do tell me :)

    Actually I think some of the confusion comes down to this. I assume as virtual cameras are just proxies, if I wanted to cast a ray, I can still use Camera.main as opposed to accessing virtual cameras. i.e. this remains valid:
    Code (CSharp):
    1. if (Physics.Raycast(rayOrigin, Camera.main.transform.forward, out hit, 1000f))
    As opposed to:

    Code (CSharp):
    1.  if (Physics.Raycast(rayOrigin, m_POVCam.transform.forward, out hit, 1000f))
     
    Last edited: Jan 30, 2019
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The first line of code will cast a ray using the main camera's forward. The second will cast using the vcam's forward. They are not the same, unless the camera is exactly tracking the vcam.

    Maybe what you want to do is: first orient your vcam so that it's looking directly at the target. Then set it's POV axes to 0, then enable it. Something like:
    Code (CSharp):
    1. vcam.rotation = Quaternion.LookRotation((target.position - vcam.position).normalized, Vector3.up);
    2. pov.m_HorizontalAxis.Value = pov.m_VerticalAxis.Value = 0;
    3. vcam.gameObejct.SetActive(true);
     
  5. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Thanks for the response. Just to clarify, vcams are just representations of where the main camera WILL move to, when a blend have finished? So if I know that a blend is finished (in this example), I know the Camera.main transform equals vcam transform?

    Equally if I used the vcam.transform.forward, that will be where my Camera.main WILL be at the end of the blend? And Camera.main (as usual) will be where the game camera transform is currently (mid blend or otherwise)?

    Cheers!
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Almost true. The vcam's transform does not necessarily hold the exact position/rotation of where the Camera will be placed. It holds what we call the "raw" or ideal position. Correction might be applied to this, for example if you have Noise enabled, or if the CM collider moves the vcam, or if you have user input from POV. Those all output to the "correction" channel, which is applied to the raw position before being assigned to the camera.

    If you want to know where the vcam indends to place the Camera, you need to access the vcam's State property. This returns a CameraState structure, which has fields for RawPosition/Orientation, CorrectionPosition/Orientation, and FinalPosition/Orientation. The Camera will be assigned FinalPosition/Orientation.

    https://docs.unity3d.com/Packages/com.unity.cinemachine@2.1/api/Cinemachine.CameraState.html
     
  7. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Ok thanks @Gregoryl Still very confused as to why this isn't working. The yellow ray in the scene view (apologies I appreciate it's a little cluttered) is pointing at the target object (Heavy1). The POV camera in question is highlighted in the hierarchy and it's transform is aligned to look down that yellow ray at the Heavy 1 - so far so good!

    However, in the game screen the POVCamera (it's called combat camera) seems to be looking straight ahead and not at Heavy1 which is on the right hand edge of the game view. It seems ridiculous the the POV camera rotated to face the target, but this somehow doesn't seem to be reflected in the game view! To be clear - I'm trying to get the POV camera to look directly at Heavy! when I transition to it (currently it's a cut).



    Any help would be appreciated! Thanks
     

    Attached Files:

    Last edited: Feb 11, 2019
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Did you set the pov's X and Y axes to 0?
     
  9. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Yeah. This is the actual code (based on yours above):
    Code (CSharp):
    1.        Debug.DrawRay(m_virtualCombatCam.transform.position, m_virtualCombatCam.transform.forward * 10000f, Color.black, 5f);
    2.  
    3.          m_virtualCombatCam.transform.rotation = Quaternion.LookRotation((targetPosition - m_virtualCombatCam.transform.position).normalized, Vector3.up);
    4.         m_POVCam.m_HorizontalAxis.Value = m_POVCam.m_VerticalAxis.Value = 0;
    5.  
    6.         Debug.DrawRay(m_virtualCombatCam.transform.position, m_virtualCombatCam.transform.forward * 10000f, Color.yellow, 5f);
    7.         m_combatCam.SetActive(true);
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Can you debug log the Value of the axes?
     
  11. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    So while the camera is active (and without moving the mouse) these are the values which are what I'd expect?
     

    Attached Files:

  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Yeah, they're basically 0, which is what you want. Do you by any chance have InheritPosition checked in CombatCam? If so, uncheck it.

    upload_2019-2-11_10-44-10.png
     
  13. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Hmmm....afraid not.
     

    Attached Files:

  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Is the CombatCam parented to anything? If so, try making it a root object.
     
  15. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    No, no parent.
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    I'm running out of ideas. Any chance you can package up a lightweight version of this project and PM it to me?
     
  17. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    Yes I will do - I appreciate that. Before I do a question - I'm assuming the follow object for the camera (and the rotation/position of it) shouldn't affect the results I'm seeing?
     
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You've got it set to HardLockToTarget, so yes it will affect the vcams's position, and since your desired rotation depends on the position then yes, it matters. FollowTarget's rotation is irrelevant. In any case, your vcam's transform seems to be set correctly, so I doubt that's the problem.
     
  19. AntLewis

    AntLewis

    Joined:
    Feb 2, 2010
    Posts:
    254
    ok, PM'd with an interesting outcome!
    Cheers
     
  20. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Sorry, but I misled you earlier. In fact, for an unparented POV vcam, the POV axis values are worldspace, not vcam local space. So your activate vcam code should be:
    Code (CSharp):
    1.     public void ActivateCombatCam( Vector3 position )
    2.     {
    3.         var euler = Quaternion.LookRotation(( position - m_virtualCombatCam.transform.position).normalized, Vector3.up).eulerAngles;
    4.         m_POVCam.m_HorizontalAxis.Value = euler.y;
    5.         m_POVCam.m_VerticalAxis.Value = euler.x;
    6.  
    7.         m_combatCam.SetActive(true);
    8.     }
    Sorry for the misinformation!
     
    raniaRMR likes this.
  21. bugbeeb

    bugbeeb

    Joined:
    Oct 19, 2021
    Posts:
    22
    Hello, I'm facing a similar problem and could use help. I have two unparented cameras, a 3rd person camera and a pov camera. I want to apply a transition from the 3rd person camera to the pov, but I want to make sure the pov camera is always in the same position before the transition is triggered. The problem is that the pov camera will freely rotate around the target when the target moves around in world space, but not update it's internal axis values. So if I rotate the target in 3rd person view, and then transition to the pov, the initial m_VerticalAxis or m_HorizontalAxis values are both zero, but the camera's position in world space has changed and therefor I need to somehow correct for the difference. Programmatically setting the pov's world position relative to the target would be ideal but doesn't seem to work. To make things more complicated, the target can rotate and move freely in any direction. This makes calculating the internal axis offset difficult because the pov could be behind or in front of the 3rd person camera relative to the origin. Any help is appreciated
     
  22. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Try enabling this option in the POV vcam:

    upload_2022-3-17_9-18-34.png
     
  23. bugbeeb

    bugbeeb

    Joined:
    Oct 19, 2021
    Posts:
    22
    @Gregoryl Thanks for the tip! I think this is the right approach given the description of that field, but it seems like I'm not accounting for a rotation somewhere because the pov is being forced in the wrong direction depending on x-z plane rotation of the target. I have set "World Up Override" on the brain to the ship's transform.
     
  24. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Hmmm that should work properly. It's difficult to reason in the abstract about this sort of thing. Could you put together a lightweight project that reproduces this camera issue, with no art (just cubes etc)? If you DM it to me I can take a look and figure out what's going on.
     
  25. bugbeeb

    bugbeeb

    Joined:
    Oct 19, 2021
    Posts:
    22
  26. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Thanks for the upload. You've uncovered a bug in POV! We will make a fix in an upcoming release.

    In the meantime, here is a workaround for you:

    1. Make the POV camera a child of the ship. The POV rotation is always relative to the vcam's parent object. If no parent, then world axes are used, and I don't think that's what you want.

    upload_2022-3-28_16-0-50.png

    2. Use this replacement for POV (it has the bug fix). Drop it into your project, it should appear in the Aim dropdown as Custom POV:

    upload_2022-3-28_16-1-51.png
     

    Attached Files:

  27. bugbeeb

    bugbeeb

    Joined:
    Oct 19, 2021
    Posts:
    22
    @Gregoryl Thank you so much. This works great. I had originally experimented with parenting the pov but got weird results which this component appears to have fixed.
     
    Gregoryl likes this.