unity 第一人称相机原地带有阻尼效果

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

public class PlayerController : MonoBehaviour
{
    public Transform target; // 目标物体
    public float distance = 10f; // 相机与目标物体的距离
    public float xSpeed = 250f; // 水平旋转速度
    public float ySpeed = 120f; // 垂直旋转速度
    public float yMinLimit = -20f; // 垂直旋转最小角度
    public float yMaxLimit = 80f; // 垂直旋转最大角度

    private float x = 0f; // 水平旋转角度
    private float y = 0f; // 垂直旋转角度
    public float smoothSpeed;

    void Start()
    {
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
    }

    void LateUpdate()
    {
        if (target)
        {
            if (Input.GetMouseButton(0))
            { // 鼠标右键按下时,根据鼠标移动改变相机角度
                x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
                y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
                y = ClampAngle(y, yMinLimit, yMaxLimit);
            }
           // distance -= Input.GetAxis("Mouse ScrollWheel") * 5f; // 鼠标滚轮控制相机距离
          //  distance = Mathf.Clamp(distance, 3f, 15f);

            // Quaternion rotation = Quaternion.Euler(y, x, 0); // 根据角度创建四元数
           //  Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position; // 根据四元数和距离计算相机位置
            Quaternion rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(y, x, 0), Time.deltaTime * smoothSpeed); // 根据当前旋转和目标旋转进行插值
           // Vector3 position = Vector3.Lerp(transform.position, rotation * new Vector3(0.0f, 0.0f, -distance) + target.position, Time.deltaTime * smoothSpeed); // 根据当前位置和目标位置进行插值
            transform.rotation = rotation;
          //  transform.position = position;
        }
    }

    static float ClampAngle(float angle, float min, float max)
    { // 将角度限制在min和max之间
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值