C# 移动摄影机在统一中抖动

C# 移动摄影机在统一中抖动,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我正在研究一个下坡赛车,我想让摄像机在不同程度上抖动,以传达玩家正在变得更快。现在我有一个名为“CameraHolder”的游戏对象,它是主摄像机的父对象 我有一个脚本附在跟随玩家的支架上,另一个脚本附在相机上,用来在支架内摇晃它 摄影机跟随脚本 using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour {

我正在研究一个下坡赛车,我想让摄像机在不同程度上抖动,以传达玩家正在变得更快。现在我有一个名为“CameraHolder”的游戏对象,它是主摄像机的父对象

我有一个脚本附在跟随玩家的支架上,另一个脚本附在相机上,用来在支架内摇晃它

摄影机跟随脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Camera cam;
    public Transform player;
    public Vector3 offset;
    public float speed = 2f;
    public Rigidbody rb; 
    public float startingFieldOfView = 60f;

    void FixedUpdate()
    {
        float interpolation = speed * Time.deltaTime;

        Vector3 cameraPosition = new Vector3(Mathf.Lerp(transform.position.x, player.position.x, interpolation), player.position.y + player.localScale.y + offset.y, player.position.z + -player.localScale.z + offset.z);
        
        // Change of camera FOV depending on the speed of the rigidbody
        transform.position = cameraPosition;
       
        cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, startingFieldOfView + (rb.velocity.magnitude / 3), .1f);
附加到子主摄影机的CameraShake脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraShake : MonoBehaviour
{
    public Rigidbody player;
    public float shakeThreshold;
    public float shakeMagnitude;

    private void Update()
    {
        float x = Random.Range(-shakeMagnitude, shakeMagnitude);
        float y = Random.Range(-shakeMagnitude, shakeMagnitude);
        if (player.velocity.magnitude >= shakeThreshold)
        {
            transform.localPosition = new Vector3(x, y, transform.position.z);
        }
    }
}

我想我可能误解了localPosition的工作原理。我以为在支架内移动时,它会随机移动相机,但它什么也没做。我是应用错了还是逻辑不正确?

我认为使用cinemachine真的会帮到你

要重新放置CameraFollow脚本,可以将虚拟摄影机与属性Follow一起使用,有关更多信息,请访问:

要更换相机抖动,可以在此处检查“噪波”属性:


有关cinemachine的更多信息,请查看此处的文档:

localposition
定义游戏对象相对于层次结构中父对象的位置。确保
shakemagnity
足够大,如果(player.velocity.magnity>=shakeThreshold)(放置断点或
Debug.Log()
而且相机组件在您正在移动的游戏对象中非常感谢您,我想我自己永远也找不到它。这正是我所需要的!