可以控制的第三人称摄像机

using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class MyDungeonCamera : MonoBehaviour {
    public GameObject target = null;
    public float rotateX = 0f;
    public float rotateY = 0f;
    public float distance = 10.0f;
    public float moveSmoothTime = 0.3f;
    public float rotateSmoothTime = 0.3f;
    public float scrollWheelSpeed = 1000.0f;
    public Vector3 targetOffset = Vector3.zero;

    private Vector3 velocity = Vector3.zero;
    private Vector3 offset = Vector3.zero;
    private const float minDistance = 5.0f;
    private const float maxDistance = 100.0f;
    private Quaternion tempRotation = Quaternion.identity;
    private Vector3 tempPosition = Vector3.zero;
    private float speedX = 120;
    private float speedY = 240;

    private float RotateDamping
    {
        get
        {
            if (rotateSmoothTime <= 0f) rotateSmoothTime = 0.001f;
            return 1f / rotateSmoothTime;
        }
    }

    void LateUpdate()
    {
        if (Input.GetMouseButton(0))
            rotateX += Input.GetAxis("Mouse X") * speedX * Time.deltaTime;
        if (Input.GetMouseButton(1))
            rotateY -= Input.GetAxis("Mouse Y") * speedY * Time.deltaTime;



        if (target == null) return;
        tempRotation = transform.rotation;
        tempPosition = transform.position;

        CalculateDistance();
        CalculateRotation();
        CalculatePosition();

        transform.rotation = tempRotation;
        transform.position = tempPosition;
    }

    private void CalculateRotation()
    {
        if (!IsNeedRotate()) return;
        Quaternion wantedRotation = Quaternion.Euler(rotateY, rotateX, 0f);
        tempRotation = Quaternion.Slerp(tempRotation, wantedRotation, Time.deltaTime * RotateDamping);
    }

    private void CalculatePosition()
    {
 
        if (!IsNeedRotate())
        {
            offset = Quaternion.Euler(rotateX, rotateY, 0f) * Vector3.forward * distance;
            Vector3 wantedPos = target.transform.position - offset + targetOffset;

            tempPosition = Vector3.SmoothDamp(tempPosition, wantedPos, ref velocity, moveSmoothTime);
        }
        else
        {
            offset = tempRotation * Vector3.forward * distance;
            tempPosition = target.transform.position - offset + targetOffset;
        }
    }

    private void CalculateDistance()
    {
        float horizontal = Input.GetAxis("Mouse ScrollWheel") * scrollWheelSpeed * Time.deltaTime;
        distance -= horizontal;
        distance = Mathf.Clamp(distance, minDistance, maxDistance);
    }

    private bool IsNeedRotate()
    {
        Vector3 eulerAngles = transform.rotation.eulerAngles;
        return !(FloatEqual(eulerAngles.x, rotateX) && FloatEqual(eulerAngles.y, rotateY));
    }

    public static bool FloatEqual(float value1, float value2)
    {
        float ret = value1 - value2;
        return ret > -0.0005f && ret < 0.0005f;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值