Center cursor without setting CursorLockMode to locked.

So I am adapting a VR game I made to a non-VR version, many of the games interactions were triggered by the player looking at gameobjects with the gaze pointer and the unity event system picking up button presses or clicks. These interactions all use a physics raycast.

So I am using mouse and keyboard controls for the non-VR version, I am using the mouse for looking around and I want to center the cursor in the center of the screen for interacting with world object.

If I use CursorLockMode.Locked then all the interactions stop working as no “mouse over” or “mouse click” events are triggered.

If I use CursorLockMode.Confined everything works and I can click on objects and all events work, but the cursor now moves around from the center which I do not want as the player really needs to also look around using the mouse.

So I need to either make events work in CursorLockMode.Locked mode, or I need to use CursorLockMode.Confined but effectively keep it centered in the screen.

Does anyone know if either of these things is possible.

Sorry if this post is confusing.

Something must be up with your ray casting.

You should have no issues locking the cursor to the center of the screen and also raycasting from the mouse cursor!

How are you raycasting?

(this is UnityScript/JS but just put it on your camera ((or the C# equivalent and you should be good to go!)) )

#pragma strict

function Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

function Update ()
{
    var cam : Transform = Camera.main.transform;
    var ray = new Ray(cam.position, cam.forward);
    var hit : RaycastHit;
 
    if(Physics.Raycast(ray, hit, 500))
    {
        if(hit.transform.gameObject.name == "SOME_OBJECT")
        {
            if(Input.GetMouseButton(0))
            {
                print("LEFT CLICKING SOME_OBJECT");
            }
        }
     
    }
    else
    {
        print("the ray is not hitting anything");
    }

}

And here it is in C# if that tickles your fancy!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseCenter : MonoBehaviour {

    // Use this for initialization
    void Start () {
        Cursor.lockState = CursorLockMode.Locked;
    }
   
    // Update is called once per frame
    void Update () {

        Transform cam = Camera.main.transform;
        Ray ray = new Ray(cam.position, cam.forward);
        RaycastHit hit;
       
        if(Physics.Raycast(ray, out hit, 500))
        {
            if(hit.transform.gameObject.name == "SOME_OBJECT")
            {
                if(Input.GetMouseButton(0))
                {
                    print("LEFT CLICKING SOME_OBJECT");
                }
            }
           
        }
        else
        {
            print("the ray is not hitting anything");
        }
    }
}

Thank you I will try this when I get home. I was using the built-in unity component “physics raycast” that I attached to the camera rather than writing my own raycast. But I think it relies on the eventsystem triggering events so this might be what is causing the issue as these events are disabled when the cursor is locked.