- Home /
How would I go about create multiple cursors controlled by multiple players
Hello! I'm trying to recreate (What I thought would be a very easy system). I simple want to have multiple cursor controlled by multiple players using either the keyboard and mouse or gamepads. A reference is the system in Ultimate chicken horse. I've so far managed to be able to move two UI objects using a mouse to control one and a gamepad to control the other but the issue here is only one will be able to detect button clicks as it is the actual mouse!
I'm sure I'm just thinking about this wrong so I will keep trying but any advice to rattle my brain would be appreciated!!
Thankyou very much.
[1]: /storage/temp/177866-2.png
Answer by jackmw94 · Mar 21, 2021 at 06:47 PM
You can manually trigger event system events using ExecuteEvents. You'll wanna make a new pointer event data object, give it the position of your FAKE cursor, raycast for all UI elements at its position then, if clicking, trigger the pointer click handler. See the code below for a bit of an example:
var pointer = new PointerEventData(EventSystem.current) {position = transform.position};
List<RaycastResult> raycastResults = new List<RaycastResult>();
_graphicRaycaster.Raycast(pointer, raycastResults);
foreach (RaycastResult raycastResult in raycastResults)
{
var ui = raycastResult.gameObject.GetComponent<UIBehaviour>();
if (ui)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log($"Clicked on {raycastResult.gameObject}");
ExecuteEvents.Execute(raycastResult.gameObject, pointer, ExecuteEvents.pointerClickHandler);
}
}
}
You'll have to get the graphic raycaster from the canvas your cursor is on and probably want to change the get space bar down check to whatever your cursors controller offers :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Standalone gets mouse input when in background UNET 1 Answer
Horizontal Input showing random decimal when not pressing anything 0 Answers
Problems with crouching (FPS/C#) 1 Answer
Pause System not working :/ 2 Answers