Hey everyone! I’m creating a 2d top-down game and currently im working on a script which should keep
the Gameobject position exactly at the same position of the mouse, but it doesn’t work.
Maybe it’s because I’m casting a float to an int, but I have seen other games where it works very well.
Problem:
When I move the mouse very fast the object glitches from it’s position
I hope anyone can help me!
void Update ()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
yMin = (int)playerRectAncor.transform.position.y - 1;
yMax = (int)playerRectAncor.transform.position.y + 1;
xMin = (int)playerRectAncor.transform.position.x - 1;
xMax = (int)playerRectAncor.transform.position.x + 1;
if (true) //Its just temporary true, for testing
{
pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
pos.z = transform.position.z;
pos.x = Mathf.Clamp(pos.x, xMin, xMax);
pos.y = Mathf.Clamp(pos.y, yMin, yMax);
// put Object position to mousePosition
rectObj.transform.position = new Vector3((int)pos.x, (int)pos.y, (int)pos.z);
}