game object attached to the cursor moving too slow
Hi, I'am trying to create a 2d gameObject containing a Sprite and a collider to replace the default cursor. To keep the game object at the cursor position I wrote a script that changes the gameObject positon to the current mousePosition
void Start () {
tr = gameObject.transform;
ZinitPos = 1.0f;
curPos = new Vector3(0.0f, 0.0f, 0.0f);
}
void Update () {
curPos = Input.mousePosition;
curPos.z = 1.0f;
tr.position = Camera.main.ScreenToWorldPoint(curPos);
}
the code is working fine but the game object has some latency, it moves very slower then the cursor. I know there is some solution some where and a somebody among you smart people :]. thank you for reading and sorry for wasting your time
Answer by paramburu · Jan 31, 2017 at 04:03 PM
Hi, Maybe you should use unity native functionality:
https://docs.unity3d.com/ScriptReference/Cursor.SetCursor.html
Good luck!
Hi @paramburu , and thanks for the reply; I know about the SetCursor function but I'am using the cursor as a game object that contains a collider, to interact with other game objects with colliders. using the default cursor or setting up a new texture for it wouldn't help me I think since I will have to calculate the distance between the position of the cursor and all the other objects with my current modelisation I have only to use OnTrigger2DEnter/Stay/Exit() to control the game the problem is that my cursor is too slow and not moving smoothly
Well, the code itself seems fine. $$anonymous$$aybe you should use the profiler to see what is happening internally. I would guess that is a performance issue of having to redraw the sprite every frame + the colliders.
Try using an independent canvas for the cursor, it should be more efficient although I don't know if that will work properly with the other colliders.
Another option could be using SetCursor and 2D raycasting or SetCursor on one side and the Collider with the same code you already have but without the texture.
Hope it helps...