I use Input.getAxisRaw to determine how far my mouse has moved and the move the camera. But it seems the camera does not move 1 to 1 with the mouse it moves faster. It’s incredibly annoying and i can’t work out why.
I can’t for the life of me work out why it moves faster the more i drag… i want to calculate how far the mouse moved from when i clicked and then update the camera movement to move that amount of distance.
I plan to smooth the movement out later for a more click feel but at moment i can’t until it moves more 1 to 1 with the mouse.
if (panning) {
Vector3 delta = new Vector3(-Input.GetAxisRaw("Mouse X", -Input.GetAxisRaw("Mouse Y"), 0.0F);
transform.position += delta * Time.deltaTime; // Use Time.delta to change it over the frames not instantly.
Vector3 position = this.transform.position;
position.z = -10.0F;
this.transform.position = position;
}
I guess i am mis understanding GetAxisRaw but i assumed that would give me the distance moved for that frame? if i then multiply by time won’t this give me the wrong distance of the mouse movement to apply to my gameObject?
Also i did not know this was a keyword in C# for unity, first time i’ve seen it used ! Neat !
GetAxisRaw just gives you a number without any smooth filtering applied to the result. it will always be 1, 0, or -1. if you move your mouse one unit in the positive direction for only one second. you’ve only traveled one unit/second but getAxisRaw will return 1 for all those frames in that one second, adding up to some number around the number of frames you had that second (it can vary).
So you moved one unit in one second on your mouse, but GetAxisRaw added up a total of 60 units in one second (again one per frame). you need to scale the number down, smooth filter it, by multiplying that result by the number of seconds that passed since the last frame. this number (deltaTime) is usually very very small, only growing large if there was a drop in frames.
GetAxisRaw simply returns the result without any filtering applied, if you want this smooth filtering try using GetAxis. GetAxis may also be a tad too smooth so you might want to scale that as well. either way works.
If i move the mouse by say 5 pixels on my screen, does that mean GetAxisRaw will still only return 1 or -1 ? Because i’m looking to collect total pixels moved from an origin point. The documentation says “units” but i can only assume that is pixels?
I find it very odd that movement of mouse and/or camera should have any relationship to deltaTime or frame counts. Surely if i move the mouse 5 pixels to the right from the origin of me pressing the mouse button down, i should have a value in my vector 3 of <5,0,0>. And if i simply did addition like:
transform.position + vector3 <5,0,0> it should move the camera exactly the same way but the camera exponentially gets faster but eventually does stop if i stop moving the mouse.
I actually come from a javascript background where by getting the mouse position on a canvas and moving it can give you the distance the mouse moved, it had no need to know total frames or deltaTime… so i am struggling to understand unity’s logic for doing this.
Ah I see what you want now, “Mouse X” and “Mouse Y” is not the mouse cursor its the actual movement of the mouse if that makes sense…for example if you move the mouse to end of the screen, “Mouse X” and “Mouse Y” would continue to register a delta because the actual mouse is still moving yet the cursor has not. To get a pixel by pixel change you can get that by caching Input.mousePosition and subtract it on the next frame the currentPosition from the last frame and that would get you a delta in pixels.
you can try to write your own…
here is something that could work. FYI I havent tried it
Yet the camera still moves super fast, can you check on your end with this code to see if you get the same? I don’t know what I am not understanding here because from the logic of it, it should move one to one with my mouse. Unless i am misunderstanding some behavior or function.