变换.position = Vector3.振动位置


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;
}
}

我有一个身体,我想让我的相机跟随我的头。它当然跟着,但它在振动,就像它不动一样,它每秒都在传送到头部。我尝试使用fixeduupdate,但它更糟。我也试过Lerp,但是Lerp使相机跟随缓慢,当我快速移动鼠标时,需要很多时间来跟随。

如果你的相机不需要任何加速或高级运动,你可以简单地将相机游戏对象作为场景层次结构(或预制)中头部游戏对象的子对象。这将使你的相机复制头部游戏物体的位置和旋转,而不需要任何编码。

如果你想让它成为第三人称视图,你可以简单地修改子位置,使它始终是父位置的局部位置。

希望这对你有帮助。

这可能是个复杂的问题。也许这些解决方案中有一个能帮上忙。

  1. 为什么你使用Input.GetAxisRaw()-这个函数返回值没有平滑滤波,尝试Input.GetAxis()作为替代。

  2. "Vibration"如果相机位置更新晚于您的身体位置,则可能会产生影响。尝试在LateUpdate()中为您的身体对象设置位置。

  3. 问题可能在旋转计算中,在你的情况下欧拉角就足够了。试着这样写:

    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);
    }
    

最新更新