- Home /
Do i need to use Time.Deltatime while using mouse axes for rotation?
so i am a bit confused i know than time.deltatime is used to make movement framerate independent but do i need to use it for mouse input i checked the docs and the example they showed did not use time.deltatime on mouse input but reading up on this subject i found out that some people say that mouse input also needs time.deltatime but my problem is that when i use time.deltatime cameras rotation in the editor is fine but when i build the game cameras rotation is ridiculously slow event if i increase its speed to 100-300 so my question is do i need to use time.deltatime when using MouseAxis or should it only be used when calculating movement.
using UnityEngine;
public class CameraRig : MonoBehaviour
{
public float m_Sensitivity = 2;
public float m_MoveSpeed = 8;
private float m_InputMouseX = 0f;
private float m_InputMouseY = 0f;
private bool m_Rotate = false;
private void GetInput()
{
m_Rotate = Input.GetMouseButton(1);
}
private void Update()
{
GetInput();
if (m_Rotate)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
m_InputMouseX += Input.GetAxis("Mouse X") * m_Sensitivity;
m_InputMouseY += Input.GetAxis("Mouse Y") * m_Sensitivity;
m_InputMouseY = Mathf.Clamp(m_InputMouseY, -90, 90);
transform.localRotation = Quaternion.AngleAxis(m_InputMouseX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(m_InputMouseY, Vector3.left);
}
else
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
#region Movement
if (Input.GetKey(KeyCode.W))
{
transform.position += transform.forward * m_MoveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
transform.position += -transform.forward * m_MoveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
transform.position += transform.right * m_MoveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position += -transform.right * m_MoveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.Q))
{
transform.position += transform.up * m_MoveSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.E))
{
transform.position -= transform.up * m_MoveSpeed * Time.deltaTime;
}
#endregion
}
}
Answer by LukasLicek · Jan 10, 2018 at 11:19 AM
Hi @Sir_Adam you don't need to use Time.deltaTime because mouse movement is relatively small per frame. So if you want to be 100% exact, you need to use Time.deltaTime, but in reality, no one will ever notice. Although it may be handy if your player would experience severe framerate drops.
But I used your script and when I implemented Time.deltaTime, Sensitivity 160 felt about right. My mouse is quite sensitive though.
Here is my edit, hope it helps :)
//Original:
m_InputMouseX += Input.GetAxis ("Mouse X") * m_Sensitivity;
m_InputMouseY += Input.GetAxis ("Mouse Y") * m_Sensitivity;
//Change:
m_InputMouseX += Input.GetAxis ("Mouse X") * m_Sensitivity * Time.deltaTime;
m_InputMouseY += Input.GetAxis ("Mouse Y") * m_Sensitivity * Time.deltaTime;
PS: Maybe you want to use axis instead of reading WASD buttons? :) Buttons can be further edited in Edit->ProjectSettings->Input
thanks for the answer! i think i won't use time.deltatime since it's not that noticeable and in builds rotation still feels extremely slow once i apply time.deltatime. PS: i use wasd because i want to implement the ability to remap keys and axis input can't be remapped wish unity would make the input module accessible :/
@Sir_$$anonymous$$ Yeah, it really bugs me :/ Would you $$anonymous$$d marking my answer as accepted? Good luck with your project!
thanks! and yeah to anyone who comes across the same problem my recommendation would be to not use Time.Deltatime while using $$anonymous$$ouse axis as its really not noticeable and most examples i came across don't use Time.Deltatime
No, you must not use deltaTime for mouse input as the mouse input is already a delta value. It's the amount the mouse has moved this frame. If you move the mouse X distance in the real world within 1 second then the value you get from the mouse axis every frame is X / framerate. For example at a framerate of 60 fps the mouse would have only moved "X / 60" between two frames while at a framerate of 30 fpx the mouse would have moved "X / 30" between two frames. In any case the accumulated movement after 1 second is always "X".
The mouse is an asynchronous input device. So it's position value changes already over time, not per frame.
So this answer is misleading. Never use deltaTime for mouse axis.
@Bunny83 I totally forgot that! Damn still a lot to learn. Apologies... Post your comment as the answer, I will delete $$anonymous$$e. Don't want to confuse more people seeing this post.