unity,如何怎么让相机平滑移动

介绍

unity,如何怎么让相机平滑移动

在这里插入图片描述


方法

ws控制前进、ad控制转向

using UnityEngine;
using System.Collections;

public class SmoothFollow : MonoBehaviour {

    // 声明变量 distance、height、heightDamping、rotationDamping 和 offsetHeight
    public float distance = 10.0f; // 相机与目标的距离
    public float height = 5.0f; // 相机与目标的高度差
    public float heightDamping = 2.0f; // 相机高度的平滑度
    public float rotationDamping = 3.0f; // 相机旋转的平滑度
    public float offsetHeight = 1.0f; // 摄像机偏移高度
    Transform selfTransform; // 自身Transform组件

    public Transform Target; // 跟随的目标对象

    void Start () {
        selfTransform = GetComponent<Transform>(); // 获取自身Transform组件
    }
    
    void LateUpdate () {
        if (!Target)
            return;

        // 计算希望的旋转角度和高度
        float wantedRotationAngle = Target.eulerAngles.y;
        float wantedHeight = Target.position.y + height;

        // 计算当前的旋转角度和高度
        float currentRotationAngle = selfTransform.eulerAngles.y;
        float currentHeight = selfTransform.position.y;

        // 平滑地旋转到希望的角度
        currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

        // 平滑地高度到希望的高度
        currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

        // 计算当前的旋转角度的四元数
        Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

        // 设置相机的位置
        selfTransform.position = Target.position;
        selfTransform.position -= currentRotation * Vector3.forward * distance;

        // 设置相机的高度
        Vector3 currentPosition = transform.position;
        currentPosition.y = currentHeight;
        selfTransform.position = currentPosition;

        // 让相机朝向目标对象
        selfTransform.LookAt(Target.position + new Vector3(0, offsetHeight, 0));
    }
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忽然602

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值