A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate
in the Unity community.
Discussion in 'Cinemachine' started by Adam_Myhill, Jan 5, 2017.
@Carrmichaelll anything that works for you is fine with us
@Adam_Myhill, @Gregoryl : does CM require a special raycast syntax of its own?
After using Cinemachine with Timeline, I'm now wondering if I should start using them outside, as to eventually replace my usual camera scripts.
But then I realised that there's an issue with a simple raycast. The returned ray has wrong position and direction when Cinemachine is in use, compared to what you would expect from :
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
clickPosition = hit.point;
Debug.DrawLine(Camera.main.transform.position, clickPosition, Color.red, 1.0f);
Obviously, in the tested situation the camera transform does match the virtual camera one, as shown in the shot, and the red line comes from the code above, when I click near the white ball...
Turning off CM and the raycast works as intended.
I can't get what's going on, really...
Cinemachine is just a way of animating the Camera that has the CM Brain. Think of it as a fancy animation tool. The camera gets positioned in LateUpdate. Raycasts should work perfectly normally.
Is it possible that you have more than one Camera in your scene, and that Camera.main is not actually the active camera?
@Gregoryl Thank you for your reply.
I tested other scenarii by aligning CMBrain update mode and my raycast function, either LateUpdate / FixedUpdate / (smart) Update.
I got expected results with both passed in LateUpdate() and FixedUpdate(). Smart Update() still returns wrong main camera position. Guess it is updating only occasionally, on event call, isn't it?
But I wonder where the main camera transform is supposed to be in between, for instance when I test without moving it. From the picture above, we could figure out that the returned camera position is a cached one, or the actual one without followOffset applied, or alike...
It seems useful to point out such corner cases, when grabbing main camera position in a different event function.
Thanks to your input, there's a workaround. I'm still interested in using Cinemachine for interactive cameras (as in the camera-control-character scheme) as well.
Setting the brain to Late/Fixed/Smart affects when the vcams track their targets and evaluate their own positions. Regardless of that setting, the Unity Camera itself is always updated in CinemachineBrain.LateUpdate(). That is when the vcam's position is applied to the Camera. CM does not move the camera at any other time, so it should remain at the previous frame's position unless you have some script that is affecting the Camera's transform. Is it possible that you're somehow moving the Camera before CM Brain's LateUpdate is called?
Ideally, your camera should be a top-level GameObject, not parented to anything, with no scripts that might alter its transform, other than CM Brain.