Search Unity

Question Getting the correct cameras transform using a path

Discussion in 'Cinemachine' started by zedz, Oct 23, 2022.

  1. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    255
    This is using a tracked dolly path camera

    if I go Cam = Main Camera
    print(Cam.name + " " + Cam.transform.position );

    then Cam.transform.position has no relation to its actual value,
    I know this to be true based on where the camera is in the world and the value in the cameras inspection transform

    so how do I get the true camera transform?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Unless you ensure that your code executes after the CMBrain has positioned the camera, you will be getting the position that was valid on the previous frame. CMBrain sets the camera position in its LateUpdate, and it also has a late execution order.
     
    zedz likes this.
  3. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    255
    The thing is its not valid last frame either, the results from Cam.transform.position are nowhere near where the camera was, it was 100s of units away. Almost like it was a different camera, but it was the same camera. checked it by printing out its name before it prints out its position.
    Even when I froze the camera path update so the camera stays in the same place, Cam.transform.position still was changing around.
    I spent hours on it, tried everything. In the end I wrote my own camera path spline tracking which uses the paths generated from cinemachine, my interpolation is not perfect but its good enough.
     
    Last edited: Oct 25, 2022
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    I don't know how to explain the strange camera position results. Either there are multiple cameras in your scene, or you have another script that is setting the camera position every frame, before the CMBrain comes along and overwrites it near the end of the frame.
     
  5. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    255
    Yes I thought that could be happening, thats why I gave the camera a name like 'camera gCXgsd6' and printed out its transform.position and name together in the console to make sure it was the same camera.