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