admin管理员组文章数量:1334337
I made a first-person camera script and it’s currently attached to the main camera which is a child of my player.
I think I made a mistake with how I made my code which I can’t solve. My mouse sensitivity is set very low, to 2f which is already a decent speed, something like 10f would be uncontrollable.
If I add * Time.deltaTime
to: float rotationSpeed = inputVector.x * mouseSensitivity;
then my vertical rotation becomes much faster than my horizontal rotation which becomes much slower. If I add * Time.deltaTime
to my mouseX and mouseY floats inside Update(), vertical mouse input/rotation no longer works and only horizontal mouse input works. But with that second approach, if I increase mouse sensitivity, horizontal input would work still, but vertical input would go from not working to being extremely jittery. Am I missing something?
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
// Camera rotation variables
public float mouseSensitivity = 2f;
public float topAngleLimit = 80.0f;
public float bottomAngleLimit = -45.0f;
private Vector2 inputVector;
public float cameraPitch = 0.0f;
public Transform mainCamera;
//public float horizontalRecoilOffset = 0f;
public Transform playerBody; // i put the main parent object into this inspector slot
[SerializeField] private WeaponManager weaponManager;
public Aiming aim;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * nouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * nouseSensitivity;
inputVector = new Vector2(mouseX, mouseY);
CameraRotation();
}
private void CameraRotation()
{
// Adjust vertical/pitch
cameraPitch -= inputVector.y;
cameraPitch = ClampAngle(cameraPitch, bottomAngleLimit, topAngleLimit);
// apply vertical rotation to the camera
mainCamera.localRotation = Quaternion.Euler(cameraPitch, horizontalRecoilOffset, 0.0f);
// Rotate the player body for horizontal move/yaw
float rotationSpeed = inputVector.x;
playerBody.Rotate(Vector3.up * rotationSpeed);
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360f) angle += 360f;
if (angle > 360f) angle -= 360f;
return Mathf.Clamp(angle, min, max);
}
}
I made a first-person camera script and it’s currently attached to the main camera which is a child of my player.
I think I made a mistake with how I made my code which I can’t solve. My mouse sensitivity is set very low, to 2f which is already a decent speed, something like 10f would be uncontrollable.
If I add * Time.deltaTime
to: float rotationSpeed = inputVector.x * mouseSensitivity;
then my vertical rotation becomes much faster than my horizontal rotation which becomes much slower. If I add * Time.deltaTime
to my mouseX and mouseY floats inside Update(), vertical mouse input/rotation no longer works and only horizontal mouse input works. But with that second approach, if I increase mouse sensitivity, horizontal input would work still, but vertical input would go from not working to being extremely jittery. Am I missing something?
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
// Camera rotation variables
public float mouseSensitivity = 2f;
public float topAngleLimit = 80.0f;
public float bottomAngleLimit = -45.0f;
private Vector2 inputVector;
public float cameraPitch = 0.0f;
public Transform mainCamera;
//public float horizontalRecoilOffset = 0f;
public Transform playerBody; // i put the main parent object into this inspector slot
[SerializeField] private WeaponManager weaponManager;
public Aiming aim;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * nouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * nouseSensitivity;
inputVector = new Vector2(mouseX, mouseY);
CameraRotation();
}
private void CameraRotation()
{
// Adjust vertical/pitch
cameraPitch -= inputVector.y;
cameraPitch = ClampAngle(cameraPitch, bottomAngleLimit, topAngleLimit);
// apply vertical rotation to the camera
mainCamera.localRotation = Quaternion.Euler(cameraPitch, horizontalRecoilOffset, 0.0f);
// Rotate the player body for horizontal move/yaw
float rotationSpeed = inputVector.x;
playerBody.Rotate(Vector3.up * rotationSpeed);
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360f) angle += 360f;
if (angle > 360f) angle -= 360f;
return Mathf.Clamp(angle, min, max);
}
}
Share
Improve this question
edited Nov 20, 2024 at 10:51
RelevantUni98
asked Nov 20, 2024 at 9:06
RelevantUni98RelevantUni98
715 bronze badges
9
|
Show 4 more comments
2 Answers
Reset to default 0Let's break down what your code does
Input.GetAxis("Mouse Y")
returns relatively small values (in pixels moved since the last frame)
you are multiplying this number by the sensitivity:
float mouseY = Input.GetAxis("Mouse Y") * currentMouseSensitivity
then storing it into the inputVector
inputVector = new Vector2(mouseX, mouseY);
then you are multiplying the Y value again when you're calculating the cameraPitch
cameraPitch -= inputVector.y * mouseSensitivity;
Check if the problem lies in the logic above
You can smooth the input over time for slightly less precision but less jitter. Using Cinemachine will do that automatically for you. Just have an empty gameobject that takes the actual mouse movements and a virtual camera with smooth follow.
本文标签: cHow to remove jittery mouse input Unity3DStack Overflow
版权声明:本文标题:c# - How to remove jittery mouse input Unity3D? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742369738a2461997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Vector2
for the mouse sensitivity so you can adjust X and Y individually? – derHugo Commented Nov 20, 2024 at 9:15rotationThreshold
.. why not take any rotation into account even if very small / slow? – derHugo Commented Nov 20, 2024 at 9:16CameraRotation
you dofloat rotationSpeed = inputVector.x * mouseSensitivity;
.. you already applied the sensitivity to theinputVector
itself so this is redundant – derHugo Commented Nov 20, 2024 at 9:17Vector2
, I will see how that goes. But I think the issue might also be with how I usecurrentMouseSensitivity
? I know not much can be helped with that since the other scripts are not included in this post. But I change sensitivity dynamically for each gun when scoping/aiming/zooming in with them. Will still try the other ideas you gave – RelevantUni98 Commented Nov 20, 2024 at 9:27if (inputVector.sqrMagnitude >= rotationThreshold)
(updated my post) so now the issue seems to be gone, but I still need to test more since I've had no luck so far, not sure why I overlooked that line though – RelevantUni98 Commented Nov 20, 2024 at 9:33