- Home /
Controlling cursor using a controller
So I am updating the controls to my game to support the controller. But I realized that I do not know how to move the cursors position with the axis of the controller. The only way I could think about doing this is by making a fake cursor and moving that instead, is this the only option?
So if I were to make a new cursor, how would I get a raycast from the camera to the new UI Image in world space? Like would use:
Ray ray = Camera.main.ScreenPointToRay(Cursor.transform.position);
This draws a line from the camera to the element, but doesn't point into the world like if I were to use Input.MousePosition.
Answer by Vilhien · Jan 04, 2019 at 08:29 PM
Seems to be unless you want to have your end user use 3rd party software ( big nope usually but I've noticed steam controller mode behave as such I thought )
Dummy target it is.. I'm in the same boat it seems.
So if we have to use a dummy target, how do I draw a raycast from the camera to the ui element in world space?
Answer by Eno-Khaon · Jan 04, 2019 at 08:41 PM
It would probably make the most sense to have a GameObject and a Vector3 for this.
The GameObject will only be a visual cue, where you'll need a billboarded shader to display a fake mouse cursor (unless you choose to do something fancier).
The Vector3, meanwhile, is your actual cursor position, driven directly by Input.mousePosition or shifted by controller input otherwise.
// After determining whether to use mouse or controller input...
if(usingMouseInput)
{
cursorPos = Input.mousePosition;
}
else if(usingControllerInput)
{
cursorPos += new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0) * cursorSpeed;
cursorPos.x = Mathf.Clamp(cursorPos.x, 0, Screen.width - 1);
cursorPos.y = Mathf.Clamp(cursorPos.y, 0, Screen.height - 1);
}
cursorObj.transform.position = Camera.main.ScreenToWorldPoint(cursorPos);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
3 person controler. rotate and turn 0 Answers
Need help with controller script. 1 Answer
Input for bluetooth controller? 0 Answers