Unity 相机平滑跟随(请指正不对的地方

  • 脚本可以挂到相机上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    
    
    public Transform player; // 跟随的游戏对象
    private Vector3 cameraOffset; // 相机的偏移

    [Range(0.01f,1.0f)] // 相机平滑度范围
    public float smoothness = 0.5f; // 初始平滑度为0.5;
    
    void Start()
    {
    
    
        cameraOffset = transform.position - player.transform.position;
        // 相机偏移位置等于相机位置减去玩家位置。
    }

    
    void Update()
    {
    
    
        Vector3 newPos = player.position + cameraOffset; // 新的位置等于玩家位置加上相机位置
        transform.position = Vector3.Slerp(transform.position, newPos, smoothness);
        // 相机的位置更新相机在a点上随着玩家b点的移动和玩家保持一样的距离设定相机偏移时的速度。
    }
}

猜你喜欢

转载自blog.csdn.net/qq_60839745/article/details/128725269