- Home /
How to make 3D object become the cursor?
I want the regular arrow cursor to be used for the majority of my game. However, when the user clicks on a button called "draw", I want the cursor to change from the arrow to a 3d pencil object I created. I then want it to draw on the blank panel wherever the user moves the mouse as clicked (like in paint or photoshop).
Answer by Ezecrafting_YT · Dec 11, 2019 at 12:40 PM
I think I could put a camera pointing at the 3D pencil and then create a raw image for that camera. Then, you can add the plain image to the canvas, hide the mouse with Cursor.visible = false
and have it follow the X and Y of the mouse. Answer if it worked or not!
(If you don't know what is a Raw Image, then search in the unity documentation).
Answer by Good_Punk · Dec 10, 2019 at 06:05 AM
User the Cursor.visible flag to hide the cursor in your script (https://docs.unity3d.com/ScriptReference/Cursor-visible.html )
Then create a script on the 3D object that moves its position in the Update method to the mouse cursor position. You can get your mouse position by transforming the screen position to world coordinates (see here: https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html).
Should look something like this:
void Update(){
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, Camera.main.nearClipPlane));
transform.position = worldPosition;
}