Wrong raycast start pos and direction with Virtual Camera on mobile device

Hello, I’m working on runner for mobile platform (testing on Android). It’s simple game where player object moving always forward. There are 2 virtual cameras in game, 1 for player (setting follow and look at when player spawning) and 2nd for finish screen. I wanted to add raycast by touching screen to interact with different objects, but it doesn’t work. I’ve started to debug and found that in editor raycast works perfectly, but on mobile device (tested on 2 devices) ray has bad origin and direction.

Editor and mobile device works with same code. It is

private void LateUpdate()
        {
            if (_gameManager.GameState.Value == GameStates.Game || _gameManager.GameState.Value == GameStates.GlassTutorial)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    Vector3 mousePosFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane);
                    Vector3 mousePosNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
                    Vector3 mousePosF = Camera.main.ScreenToWorldPoint(mousePosFar);
                    Vector3 mousePosN = Camera.main.ScreenToWorldPoint(mousePosNear);
                    var go = new GameObject();
                    var line = go.AddComponent<LineRenderer>();
                    line.widthMultiplier = 0.1f;
                    line.SetPosition(0, mousePosN);
                    line.SetPosition(1, (mousePosF - mousePosN) * 500f);
                    Debug.DrawRay(mousePosN, (mousePosF - mousePosN) * 500f, Color.red, 5f);
                    Debug.Log($"Origin = {mousePosN} | Direction = {(mousePosF - mousePosN)} ");
                    if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out var hit, 500f, LayerMask.GetMask("Obstacle")))
                    {
                      // some staff
                    }
                }
            }
        }

I also tryed:

  • Change update method in Cinemachine Brain
  • Change script excecution order
  • Change between Update, FixedUpdated and LateUpdate in my script
  • Use Ray ray = Camera.main.ScreenToWorldPoint(Input.mousePosition); (This perfectly works in editor as the code above)

Debug log tells that origin always stays in the same position, but player always moving forward.

Logs about raycast and touch/click positions here : ScreenPointToRay Difference - Pastebin.com

Disabling cinemachine brain and virtual cameras (using default camera instead) fixed the problem, but I want to know how to make it works together, because I need cinemachine in my project.

Hope someone can help me with this.
Thanks

This problem can occur when your script runs before Cinemachine has updated the Camera’s position, so the raycast is using stale data from the previous frame. Cinemachine updates the camera in CinemachineBrain.LateUpdate (it doesn’t matter what the update mode for the brain is - it always pushes to the camera transform in LateUpdate). Did you set the script execution order of your script to be later than CinemachineBrain? That should work.

Alternatively, you can add a callback to CinemachineCore.CameraUpdatedEvent, which is invoked just after Cinemachine sets the camera position. Do your raycast there.

Hi, I have similar issue while ray casting from component added in runtime. it looks like camera position gets frozen rendering ray cast useless.
wrong camera position:


correct behavior when script is added before hand:

unity 2021.2.11f1 Cinemachine 2.8.4

7988910--1026495--upload_2022-3-23_22-1-28.png

I can also reproduce this by setting enabled = false on my script either in inspector or in code camera position is correct until I do so. Camera moves for few frames at the start so the lines at the right reflected correct position in time. The cluster of rays at the center is generated after script is enabled again.

If you do your raycasts from within a callback registered to CinemachineCore.CameraUpdatedEvent, everything should work correctly.