2

I have a rigidbody that I want to move with a mouse using Input.GetAxisRaw.
I know that Input.GetAxis is framerate independent but in my case it starts to move a lot faster at lower framerates. I have a suspicion that it is so because of the Rigidbody.MovePosition because it doesn't occur when I set the position directly. Dividing the mouse input by Time.DeltaTime does fix it but I'm not sure whether that's a good solution. Also I'm not sure whether that's related the input starts to get really jerky when the framerate is very high (~1000) and input sensitivity can get drastically different on different pc's.
My game is 2d with top down view.
Here's my code:

private void Update() { mouse_movement = GetMouseMovement(); } private void FixedUpdate() { Vector3 new_position = transform.position + mouse_movement; rigidbody.MovePosition(new_position); } public Vector3 GetMouseMovement() { Vector3 mouse_movement = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")); return mouse_movement * MouseSensitivity; } 

Thanks in advance.

4
  • Yeah, ummm, that's the whole point to Time.deltaTime. So your game is framerate independent. You need to return mouse_movement * MouseSensitivity * Time.deltaTime. That's pretty standard. Commented Jun 2, 2020 at 18:11
  • I would use ScreenPointToRay method to determine where to move the object docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html Commented Jun 2, 2020 at 18:17
  • Also read out your mouse_movement in FixedUpdate and not in plain Update, so you become fully frame rate independent :), cos fixedUpdate is called in a fixed intervall unlike Update Commented Jun 2, 2020 at 18:39
  • Right now I can achieve the closest to it being f.r. independent is by calculating input like this return mouse_movement * MouseSensitivity/(300f*Time.deltaTime); and doing everything in regular Update But at higher framerates it starts stuttering (I guess that's because fps starts to jump up and down). Commented Jun 2, 2020 at 19:09

1 Answer 1

2
  1. If you struggling from too high FPS, you can set target framerate

  2. you get your input in update, and apply it in fixed update. It means, mouse position sampled once per frame, but then applied several times between frames - on FixedUpdate. So you ARE framerate dependant. To be really framerate independant, do input check in FixedUpdate too. Or move your object in Update, multiplying value by Time.deltaTime. Just do not mix Update and FixedUpdate.

  3. You get mouse position on screen. It means, you are also resolution-dependant. There are two ways to fix: convert mouse position into world space, or calculate mouse movement as a percentage from screen size:

    movementX = Input.GetAxisRaw("Mouse X") / Screent.width;

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.