Unity 相机在屏幕上滑动,第一人称 [英] Unity camera flicks across screen, first person
问题描述
您好,我是 UNITY 的新手,我为第一人称相机编写了一些代码,但是有时当我环顾四周时,它会轻弹相机面向的方向.这是我的代码
Hello I am new to UNITY I have written some code for a first person camera however sometimes when I am looking around it will flick what way the camera is facing. This is my code
public float mouseSensitivity = 200f;
public Transform playerBody;
float yRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
yRotation -= mouseY;
yRotation = Mathf.Clamp(yRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(yRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
我有一个视频来展示我的问题https://youtu.be/nMopJzNyYr4问题不清楚.
I have a video to show my issue as well https://youtu.be/nMopJzNyYr4 for is the question is not clear.
我有一个胶囊作为我的父母,它是玩家的实际身体,然后我的相机是 playerBody 的孩子(我的胶囊)
I have a capsule as my parent it is the players actual body and then my camera is a child to the playerBody ( my capsule )
推荐答案
尝试不用 deltaTime
乘法.鼠标轴上的 GetAxis
为您提供鼠标位置的差异,除非您对 deltaTime 或任何其他时间增量="https://en.wikipedia.org/wiki/Absement" rel="nofollow noreferrer">Absement 你可能不是!
Try it without the deltaTime
multiplication. GetAxis
on a mouse axis gives you a difference in mouse position, which should not be multiplied by deltaTime
or any other time delta unless you are interested in a measurement of Absement which you probably are not!!
您对距离测量更感兴趣,因此您可以使用float mouseABC = Input.GetAxis("Mouse ABC") * mouseSensitivity;
.
You are more interested in a distance measurement, so you can just use
float mouseABC = Input.GetAxis("Mouse ABC") * mouseSensitivity;
.
或者,如果您想要鼠标移动的速度,您实际上宁愿除以time.deltaTime
,例如float mouseHorizontalVelocity = Input.GetAxis("Mouse X") * mouseSensitivity/Time.deltaTime;
.
Alternatively, if you were to want the speed of the mouse movement, you would actually rather divide by time.deltaTime
such as float mouseHorizontalVelocity = Input.GetAxis("Mouse X") * mouseSensitivity / Time.deltaTime;
.
这篇关于Unity 相机在屏幕上滑动,第一人称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!