- Home /
Changing cursor using 2D sprites
I would like to change my cursor in the game, using 2D sprites. The problem is that the script I used is working only on 2D game, not on 3D. Somebody help?
Here is the script i atached to the 2D sprite:
public class CursorScript : MonoBehaviour {
// Use this for initialization
void Start () {
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
Vector2 cursorPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.position = cursorPos;
}
}
I would really apreciate some help. Thx!
Answer by Harinezumi · Jul 18, 2018 at 09:15 AM
For custom cursors I would rather recommend using the Cursor API, specifically the Cursor.SetCursor() call. Note that you will have to set your cursor images' texture type as "Cursor" rather than "Sprite" to use it with this API.
However, if you really want to stick with the above approach for some reason, you will either have to map the mouse position to an appropriate 3D point visible by the camera, or use the sprite as UI element on a canvas, and map the mouse position to the appropriate point on the canvas. However, I don't recommend this approach, because there will always be a small difference between the position of the cursor image and the real position of the mouse pointer. This problem is correctly handled by the Cursor API, and that's why I recommended it first.
Actually with Cursor API even if you set image as "Sprite" it will work, but in both cases there is an issue. The actual cursor pointer is far above the texture which is set as image. From attach screenshot red dot is actual mouse pointer.![alt text][1] [1]: /storage/temp/179461-screen-shot-2021-04-19-at-222355.png
Your answer
![](https://koobas.hobune.stream/wayback/20220612170954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Blur a sprite depending on its Order In Layer 0 Answers
I'm experiencing a weird effect with my 2D light. Any fix? 1 Answer
Sprite cutoff/altered in game view but not scene view 0 Answers
Default Contact Offset Problems 1 Answer
Universal Render Pripeline under 2019 LTS not rendering objects : Game & Scene windows 0 Answers