相机旋转缩放平移功能

开发对象的旋转,平移,缩放查看功能

之前项目做的旋转平衡和缩放功能受物体中心点的控制,效果也不是很好,容易导致相机效果变形,现在查到一篇文章是通过相机来实现,真的非常好用,可以自行扩展源码,下面贴出地址:
原文地址
如果原作者文章移除,可以直接查看我贴出的代码内容,可以直接将此代码挂到相机上使用。

using UnityEngine;
using System.Collections;

public class FreeCameraController : MonoBehaviour
{
    //摄像机绕屏幕中心旋转缩放平移脚本  

    public float thetaSpeed = 250.0f;
    public float phiSpeed = 120.0f;
    public float moveSpeed = 10.0f;
    public float zoomSpeed = 30.0f;

    public float phiBoundMin = -89f;
    public float phiBoundMax = 89f;
    public bool useMoveBounds = true;
    public float moveBounds = 100f;

    public float rotateSmoothing = 0.5f;
    public float moveSmoothing = 0.7f;

    public float distance = 2.0f;
    private Vector2 euler;

    private Quaternion targetRot;
    private Vector3 targetLookAt;
    private float targetDist;
    private Vector3 distanceVec = new Vector3(0, 0, 0);

    private Transform target;
    private Rect inputBounds;
    public Rect paramInputBounds = new Rect(0, 0, 1, 1);

    public Vector3 pivotPoint = new Vector3(0, 2, 0);

    public void Start()
    {
        Vector3 angles = transform.eulerAngles;
        euler.x = angles.y;
        euler.y = angles.x;
        //unity only returns positive euler angles but we need them in -90 to 90  
        euler.y = Mathf.Repeat(euler.y + 180f, 360f) - 180f;

        GameObject go = new GameObject("_FreeCameraTarget");
        go.hideFlags = HideFlags.HideAndDontSave | HideFlags.HideInInspector;
        target = go.transform;


        target.position = pivotPoint;
        targetDist = (transform.position - target.position).magnitude;

        targetRot = transform.rotation;
        targetLookAt = target.position;
    }

    public void Update()
    {
        //NOTE: mouse coordinates have a bottom-left origin, camera top-left  
        inputBounds.x = GetComponent<Camera>().pixelWidth * paramInputBounds.x;
        inputBounds.y = GetComponent<Camera>().pixelHeight * paramInputBounds.y;
        inputBounds.width = GetComponent<Camera>().pixelWidth * paramInputBounds.width;
        inputBounds.height = GetComponent<Camera>().pixelHeight * paramInputBounds.height;

        if (target && inputBounds.Contains(Input.mousePosition))
        {
            float dx = Input.GetAxis("Mouse X");
            float dy = Input.GetAxis("Mouse Y");

            bool click1 = Input.GetMouseButton(1);
            bool click2 = Input.GetMouseButton(2);


            if (click2)
            {
                dx = dx * moveSpeed * 0.005f * targetDist;
                dy = dy * moveSpeed * 0.005f * targetDist;
                targetLookAt -= transform.up * dy + transform.right * dx;
                if (useMoveBounds)
                {
                    targetLookAt.x = Mathf.Clamp(targetLookAt.x, -moveBounds, moveBounds);
                    targetLookAt.y = Mathf.Clamp(targetLookAt.y, -moveBounds, moveBounds);
                    targetLookAt.z = Mathf.Clamp(targetLookAt.z, -moveBounds, moveBounds);
                }
            }

            else if (click1)
            {
                dx = dx * thetaSpeed * 0.02f;
                dy = dy * phiSpeed * 0.02f;
                euler.x += dx;
                euler.y -= dy;
                euler.y = ClampAngle(euler.y, phiBoundMin, phiBoundMax);
                targetRot = Quaternion.Euler(euler.y, euler.x, 0);
            }

            targetDist -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed * 0.5f;
            targetDist = Mathf.Max(0.1f, targetDist);
        }
    }

    public void FixedUpdate()
    {
        distance = moveSmoothing * targetDist + (1 - moveSmoothing) * distance;

        transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, rotateSmoothing);
        target.position = Vector3.Lerp(target.position, targetLookAt, moveSmoothing);
        distanceVec.z = distance;
        transform.position = target.position - transform.rotation * distanceVec;
    }

    static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360f) angle += 360f;
        if (angle > 360f) angle -= 360f;
        return Mathf.Clamp(angle, min, max);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

瓜皮肖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值