- Home /
[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int
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);
}
Well, so you know, you can call update more than once a frame. But a better idea would be to smooth out the motion. Lastly, just guessing here, but maybe it is because you're casting to int. Floats give you the space in between values, ints will jerk from one value to another.
Why are you even casting to int? The Vector3 constructor can take floats. Also the $$anonymous$$athf.Clamp takes floats: $$anonymous$$athf.Clamp
$$anonymous$$aybe the glitching comes from the int cast, 1.5 for example will get the 5 just cut off I guess when casting ins$$anonymous$$d of rounding.
So when you move the mouse across X/Y 1.0 -> X/Y 1.5 -> X/Y 2.1 it will move from X/Y 1 -> X/Y 1 -> X/Y 2
Actually i don't get the whole point of all that code. Since he said the glitching only happens when he moves the mouse fast, that is of course related to the clamping. He clamps the new position to be in a rectangle (-1,-1,1,1) around the last position. So if the mouse moves further out the position gets clamped. This would cause the object to lag behind as each frame the position gets closer to the actual mouse position.
So i have no idea why he doesn't just do:
rectObj.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
The int casting may be used to actually hit direct pixels. Though this would require an orthographic / 2d camera with a size equal to the screen height.
Answer by Neoletum · Jan 14, 2018 at 12:59 PM
Hey, thank you for your answer, it's difficult for me to describe my problem in english, maybe I described it bad, the point is that the game is tiled-base, and I'm casting it to an int because the whiteRect should only move in integers so the player can only build in a certain range around him.
In a nutshell, player should only be able to move the whiteRect in a 3x3 Range around his character otherwise he would be able to dig a hole out of his range^^ And moving the rect without casting it to an int seems not suitable to the kind of game im trying to create.
Here is a picture, it's like building in Stardew Valley:
So, I found the answer, the Problem was that a cast doesnt round up like I expected it. When the number becomes negativ it rounds in an other way, like this:
(int)2.5 = 2 (int)(-2.5) = -2 but it has to become -3!
Solution: Using $$anonymous$$athf.Floor(-2.5) rounds up to -3 and everything works fine.
Thank you all!
Thats what I tried to tell you. You are not rounding but casting so it cuts off the numbers after the dot. 2.1 becomes 2, -2.1 becomes -2. With the usage of floor you will run into other Problems. Look at how $$anonymous$$athf.Floor behaves:
Debug.Log($$anonymous$$athf.Floor(10.0F)); // returns 10
Debug.Log($$anonymous$$athf.Floor(10.2F)); // returns 10
Debug.Log($$anonymous$$athf.Floor(10.7F)); // returns 10
Debug.Log($$anonymous$$athf.Floor(-10.0F)); // returns -10
Debug.Log($$anonymous$$athf.Floor(-10.2F)); // returns -11
Debug.Log($$anonymous$$athf.Floor(-10.7F)); // returns -11
If that is what you want thats great, otherwise you should look at the rounding functions.
Also you should use FloorToInt ins$$anonymous$$d of Floor so you dont have to cast, because it returns an int
Yes, but it works now like I expected it, thank you for your hints, when you answer not as a comment I can accept your answer =)