[英]transform.position = Vector3.position vibrates
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraSc : MonoBehaviour
{
public float sens;
public Transform body;
public Transform head;
float xRot = 0;
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float x = Input.GetAxisRaw("Mouse X") * sens * Time.deltaTime;
float y = Input.GetAxisRaw("Mouse Y") * sens * Time.deltaTime;
xRot -= y;
xRot = Mathf.Clamp(xRot,-80f,80f);
transform.localRotation = Quaternion.Euler(xRot,0f,0f);
body.Rotate(Vector3.up, x);
transform.position = head.position;
//transform.rotation = head.rotation;
}
}
我有一个身体,我希望我的相机跟随我的头。 它当然会跟随,但它会振动,就像它没有移动一样,它每秒都在传送到头部。 我尝试使用 FixedUpdate,但情况更糟。 我也尝试过 Lerp,但 lerp 使相机跟随速度变慢,当我快速移动鼠标时,它需要很长时间才能跟随。
如果你的相机不需要任何加速或高级移动,你可以简单地让你的相机游戏对象成为场景层次结构(或预制件)中头部游戏对象的子对象。 这将使您的相机复制 position 和头部游戏对象的旋转而无需任何编码。
如果你想让它成为第三人称视角,你可以简单地修改孩子 position ,它永远是父母的本地人。
希望这会有所帮助。
这可能是个复杂的问题。 也许这些解决方案之一可以提供帮助。
为什么使用Input.GetAxisRaw()
- 这个 function 返回值没有平滑过滤,请尝试Input.GetAxis()
作为替代方法。
如果相机 position 更新晚于您的身体 position,则可能会出现“振动”效果。尝试在LateUpdate()
中为您的身体 object 设置 position。
问题可能出在旋转计算中,在您的情况下,欧拉角就足够了。 尝试使用这样的东西:
public float sens = 100.0f; public float minY = -45.0f; public float maxY = 45.0f; private float rotationY = 0.0f; private float rotationX = 0.0f; private void Update() { rotationX += Input.GetAxis("Mouse X") * sens * Time.deltaTime; rotationY += Input.GetAxis("Mouse Y") * sens * Time.deltaTime; rotationY = Mathf.Clamp(rotationY, minY, maxY); transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.