Unity实现3D模式下的摄像机视角控制

方法1:旋转+缩放

效果:
在这里插入图片描述
code:

using UnityEngine;
public class maxCamera : MonoBehaviour
{
    public Transform target;
    public Vector3 targetOffset;

    private float distance = 0f;

    /// <summary>
    /// 最远距离:数值越大,可以呈现的效果越小
    /// </summary>
    public float maxDistance = 15;
    /// <summary>
    /// 最近距离:数值越小,可以呈现的效果越大
    /// </summary>
    public float minDistance = 2f;

    private float xSpeed = 100.0f;
    private float ySpeed = 100.0f;

    //yMinLimit=-85
    //yMaxLimit=85 
    //可以看到物体底部

    public int yMinLimit = 0;
    public int yMaxLimit = 90;

    public int zoomRate = 40;
    public float panSpeed = 0.3f;
    public float zoomDampening = 5.0f;

    private float xDeg = 0.0f;
    private float yDeg = 0.0f;
    private float currentDistance;
    private float desiredDistance;
    private Quaternion currentRotation;
    private Quaternion desiredRotation;
    private Quaternion rotation;
    private Vector3 position;

    void Start() { Init(); }
    void OnEnable() { Init(); }

    public void Init()
    {
        if (!target)
        {
            GameObject go = new GameObject("Cam Target");
            go.transform.position = transform.position + (transform.forward * distance);
            target = go.transform;
        }

        distance = Vector3.Distance(transform.position, target.position);
        currentDistance = distance;
        desiredDistance = distance;

        position = transform.position;
        rotation = transform.rotation;
        currentRotation = transform.rotation;
        desiredRotation = transform.rotation;

        xDeg = Vector3.Angle(Vector3.right, transform.right);
        yDeg = Vector3.Angle(Vector3.up, transform.up);
    }

    void LateUpdate()
    {
        if (Input.GetMouseButton(1) || Input.GetMouseButton(2))
        {
            xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
        }
        yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);

        desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
        currentRotation = transform.rotation;


        rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
        transform.rotation = rotation;

        desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
        desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
        currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);

        position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
        transform.position = position;
    }

    private static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}
方法2:旋转+缩放+平移

效果:
在这里插入图片描述
code:

using UnityEngine;

public class CamOperator : MonoBehaviour
{
    public CamOperator Instance;
    //先确定相机围绕哪个物体进行旋转,缩放,平移;也就是需要确定相机的父物体
    //先写右键旋转(相机),确定相机旋转的速度
    public Transform target;
    public float suoFang = 1.5f;//缩放速度      1.5
    public float pingYi = 0.1f;    //平移速度      0.1
    //旋转的初始角度
    private float x = 0.0f;
    private float y = 0.0f;
    //相机旋转的速率
    public float xuanZhuan = 6.0f;
    //获取相机Transform对象
    private Transform cam;
    //相机距离目标物体的起始距离(距离设置为负值,是因为相机是在目标物体的正后方)
    private float distance = -6;
    //相机距离的物体的最近和最远距离
    private float minDistance = -3.5f;
    private float maxDistance = -6;
    //初始位置
    private float camPostion_x = 0;
    private float camPostion_y = 0;
    void Awake()
    {
        Instance = this;
        Screen.fullScreen = true;
        transform.position = target.position;
        transform.rotation = Quaternion.Euler(y, x, 0);
        cam = transform.FindChild("Main Camera");
        cam.localPosition = new Vector3(0, 0, distance);
    }
    void Update()
    {
        if (Input.GetMouseButton(1))
        {
            //右键按下
            x += Input.GetAxis("Mouse X") * xuanZhuan * 0.5f;

            y -= Input.GetAxis("Mouse Y") * xuanZhuan * 0.5f;

            x = ClampAngle(x, -100, 100);

            y = ClampAngle(y, -10, 70);

            var rotation = Quaternion.Euler(y, x, 0);

            transform.rotation = rotation;
        }
        else if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            //中键滚动缩放
            distance += Input.GetAxis("Mouse ScrollWheel") * suoFang;
            distance = Mathf.Clamp(distance, maxDistance, minDistance);
            cam.localPosition = new Vector3(camPostion_x, camPostion_y, distance);
        }
        //中键按下平移
        if (Input.GetMouseButton(2))
        {
            camPostion_x -= Input.GetAxis("Mouse X") * pingYi;
            camPostion_y -= Input.GetAxis("Mouse Y") * pingYi;
            cam.localPosition = new Vector3(camPostion_x, camPostion_y, distance);
        }

    }

    static float ClampAngle(float angle, float minAngle, float maxAngle)
    {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, minAngle, maxAngle);
    }
}

使用方法:
1,新建一个空物体,将摄像机设置为这个空物体的子层级,Transform位置均设置为原点。
2,指定目标物体

(不推荐:缩放旋转无阻尼效果,交互体验不如方法1,而且平移功能基本不会用到)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值