相机漫游,绕物体旋转及缩放(Android及手机端通用)

 缩放有两种方式:一种改变相机的视距还有一种直接改变相机的位置们这里我们使用第二种方法;

旋转:绕自身旋转和绕物体旋转, 这里我们也是使用第二种;  

其中的阀值需要自己控制。 有一些相机的角度位置离物体中心点比较远  ,这时候的可变参数就需要调大一点。好了废话不多说 直接怼代码;

啦啦啦啦  加油加油

#region HeadComments
// ********************************************************************
//  Copyright (C) 2018 DefaultCompany
//  作    者:Evan
//  文件路径:Assets/Scripts/TouchCamera.cs
//  创建日期:2018/10/11 17:43:21
//  功能描述:
//
// *********************************************************************
#endregion

using UnityEngine;
using System.Collections;

public class TouchCamera : MonoBehaviour
{
    public bool canRotation_X = true;
    public bool canRotation_Y = true;
    public bool canScale = true;
    #region Field and Property

    /// 
    /// Around center.  围绕旋转中心点
    /// 
    public Transform target;

    /// 
    /// 滑动的速度值
    /// 
    [Range(0, 5)]
    public float mMouseScrollSpeed = 5;

    /// 
    /// Settings of mouse button, pointer and scrollwheel.
    /// 
    public MouseSettings mouseSettings;

    /// 
    /// 角度限制
    /// 
    public Range angleRange = new Range(20, 90);

    /// 
    /// 距离中心点距离限制
    /// 
    public Range distanceRange = new Range(0.7f, 2.5f);

    /// 
    /// 阻尼值 
    /// 
    [Range(0, 10)]
    public float damper = 5;

    /// 
    /// 双指滑动每秒滑动的阀值
    /// 
    [Range(0, 0.1f)]
    public float mScaleSpeed = 0.002f;

    /// 
    /// 相机切换视角的过渡速度
    /// 
    private float mCameraTime = 16f;

    /// 
    /// 旋转阀值
    /// 
    [Range(0, 5)]
    public float mMousePointSpeed = 3;

    /// 
    /// Camera current angls.
    /// 相机当前的角度值
    /// 
    public Vector2 CurrentAngles { protected set; get; }

    /// 
    /// Current distance from camera to target.
    /// 
    public float CurrentDistance { protected set; get; }

    /// 
    /// Camera target angls.
    /// 
    protected Vector2 targetAngles;


    /// 
    /// Target distance from camera to target.
    /// 
    protected float targetDistance;
    #endregion

    private GameObject mSetTargetObjct;

    /// 
    /// 镜头拉近
    /// 
    private bool mHasSetPoint = false;

    #region Protected Method
    protected virtual void Start()
    {
        mouseSettings = new MouseSettings(0, mMousePointSpeed, mMouseScrollSpeed);
        CurrentAngles = targetAngles = transform.eulerAngles;
        CurrentDistance = targetDistance = Vector3.Distance(transform.position, target.position);
    }

    /// 
    /// 镜头拉近
    /// 
    /// 设置拉近的位置
    public void SetPointObject(GameObject point)
    {
        mSetTargetObjct = point;
        mHasSetPoint = true;
    }

    protected virtual void LateUpdate()
    {
        target.transform.localPosition = Vector3.zero;
        //镜头拉近效果显示
        if (mHasSetPoint)
        {
            OnSetPointLerpCamera();
            return;
        }

#if UNITY_EDITOR
        AroundByMouseInput();
#elif UNITY_ANDROID || UNITY_IPHONE
 
        AroundByMobileInput();
#endif
    }

    private float currentTime = 0;

    /// 
    /// 镜头拉近
    /// 
    private void OnSetPointLerpCamera()
    {
        currentTime += 1/mCameraTime * Time.deltaTime;
        if (Vector3.Distance(transform.position, mSetTargetObjct.transform.position) > 0.02f)
        {
            transform.position = Vector3.Lerp(transform.position, mSetTargetObjct.transform.position, currentTime);
            transform.localEulerAngles = Vector3.Lerp(transform.localEulerAngles, mSetTargetObjct.transform.localEulerAngles, currentTime);
        
        }
        else {
       
            transform.position = mSetTargetObjct.transform.position;
            transform.localEulerAngles = mSetTargetObjct.transform.localEulerAngles;
            mHasSetPoint = false;
            currentTime = 0f;
        }

        CurrentAngles = targetAngles = transform.localEulerAngles;
        CurrentDistance = targetDistance = Vector3.Distance(transform.position, target.position);
        CurrentDistance = targetDistance = Mathf.Clamp(targetDistance, distanceRange.min, distanceRange.max);
    }

    //记录上一次手机触摸位置判断用户是在左放大还是缩小手势  
    private Vector2 oldPosition1;
    private Vector2 oldPosition2;

    private bool m_IsSingleFinger;

    /// 
    ///  手指滑动
    /// 
    protected void AroundByMobileInput()
    {
        if (Input.touchCount == 1)
        {

            if (Input.touches[0].phase == TouchPhase.Moved)
            {
                targetAngles.y += Input.GetAxis("Mouse X") * mouseSettings.pointerSensitivity;
                targetAngles.x -= Input.GetAxis("Mouse Y") * mouseSettings.pointerSensitivity;

                //Range.
                targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
            }

            m_IsSingleFinger = true;
        }

        //Mouse scrollwheel.
        if (canScale)
        {
            if (Input.touchCount > 1)
            {


                //计算出当前两点触摸点的位置  
                if (m_IsSingleFinger)
                {
                    oldPosition1 = Input.GetTouch(0).position;
                    oldPosition2 = Input.GetTouch(1).position;
                }


                if (Input.touches[0].phase == TouchPhase.Moved && Input.touches[1].phase == TouchPhase.Moved)
                {
                    var tempPosition1 = Input.GetTouch(0).position;
                    var tempPosition2 = Input.GetTouch(1).position;


                    float currentTouchDistance = Vector3.Distance(tempPosition1, tempPosition2);
                    float lastTouchDistance = Vector3.Distance(oldPosition1, oldPosition2);

                    //计算上次和这次双指触摸之间的距离差距  
                    //然后去更改摄像机的距离  
                    targetDistance -= (Mathf.Clamp((currentTouchDistance - lastTouchDistance), -mScaleSpeed, mScaleSpeed))* mouseSettings.wheelSensitivity;

                    //备份上一次触摸点的位置,用于对比  
                    oldPosition1 = tempPosition1;
                    oldPosition2 = tempPosition2;
                    m_IsSingleFinger = false;
                }
            }

        }

        targetDistance = Mathf.Clamp(targetDistance, distanceRange.min, distanceRange.max);

        //Lerp.
        CurrentAngles = Vector2.Lerp(CurrentAngles, targetAngles, damper * Time.deltaTime);
        CurrentDistance = Mathf.Lerp(CurrentDistance, targetDistance, damper * Time.deltaTime);




        if (!canRotation_X) targetAngles.y = 0;
        if (!canRotation_Y) targetAngles.x = 0;


        //更新物体的旋转角度及位置信息
        transform.rotation = Quaternion.Euler(CurrentAngles);

        transform.position = target.position - transform.forward * CurrentDistance;

        if (transform.position.y <= 0.2f)
        {
            transform.position = new Vector3(transform.position.x, 0.2f, transform.position.z);
            return;
        }
    }

    /// 
    /// 键盘滑动
    /// 
    protected void AroundByMouseInput()
    {
        if (Input.GetMouseButton(mouseSettings.mouseButtonID))
        {
            //Mouse pointer.
            targetAngles.y += Input.GetAxis("Mouse X") * mouseSettings.pointerSensitivity;
            targetAngles.x -= Input.GetAxis("Mouse Y") * mouseSettings.pointerSensitivity;

            //Range.
            targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
        }

        //Mouse scrollwheel.
        if (canScale)
        {

            targetDistance -= Input.GetAxis("Mouse ScrollWheel") * mouseSettings.wheelSensitivity;
        }
        // m_debugTip.text = Input.GetAxis("Mouse ScrollWheel").ToString() + " + " + targetDistance.ToString();


        targetDistance = Mathf.Clamp(targetDistance, distanceRange.min, distanceRange.max);

        //Lerp.
        CurrentAngles = Vector2.Lerp(CurrentAngles, targetAngles, damper * Time.deltaTime);
        CurrentDistance = Mathf.Lerp(CurrentDistance, targetDistance, damper * Time.deltaTime);

        if (!canRotation_X) targetAngles.y = 0;
        if (!canRotation_Y) targetAngles.x = 0;
        transform.rotation = Quaternion.Euler(CurrentAngles);
        transform.position = target.position - transform.forward * CurrentDistance;
    }
    #endregion
}
[SerializeField]
public struct MouseSettings
{
    /// 
    /// ID of mouse button.
    /// 
    public int mouseButtonID;

    /// 
    /// Sensitivity of mouse pointer.
    /// 
    public float pointerSensitivity;

    /// 
    /// Sensitivity of mouse ScrollWheel.
    /// 
    public float wheelSensitivity;

    /// 
    /// Constructor.
    /// 
    /// ID of mouse button.
    /// Sensitivity of mouse pointer.
    /// Sensitivity of mouse ScrollWheel.
    public MouseSettings(int mouseButtonID, float pointerSensitivity, float wheelSensitivity)
    {
        this.mouseButtonID = mouseButtonID;
        this.pointerSensitivity = pointerSensitivity;
        this.wheelSensitivity = wheelSensitivity;
    }
}

public struct Range
{
    /// 
    /// Min value of range.
    /// 
    public float min;

    /// 
    /// Max value of range.
    /// 
    public float max;

    /// 
    /// Constructor.
    /// 
    /// Min value of range.
    /// Max value of range.
    public Range(float min, float max)
    {
        this.min = min;
        this.max = max;
    }
}

public struct PlaneArea
{
    /// 
    /// Center of area.
    /// 
    public Transform center;

    /// 
    /// Width of area.
    /// 
    public float width;

    /// 
    /// Length of area.
    /// 
    public float length;

    /// 
    /// Constructor.
    /// 
    /// Center of area.
    /// Width of area.
    /// Length of area.
    public PlaneArea(Transform center, float width, float length)
    {
        this.center = center;
        this.width = width;
        this.length = length;
    }
}

public struct AlignTarget
{
    /// 
    /// Center of align target.
    /// 
    public Transform center;

    /// 
    /// Angles of align.
    /// 
    public Vector2 angles;

    /// 
    /// Distance from camera to target center.
    /// 
    public float distance;

    /// 
    /// Range limit of angle.
    /// 
    public Range angleRange;

    /// 
    /// Range limit of distance.
    /// 
    public Range distanceRange;

    /// 
    /// Constructor.
    /// 
    /// Center of align target.
    /// Angles of align.
    /// Distance from camera to target center.
    /// Range limit of angle.
    /// Range limit of distance.
    public AlignTarget(Transform center, Vector2 angles, float distance, Range angleRange, Range distanceRange)
    {
        this.center = center;
        this.angles = angles;
        this.distance = distance;
        this.angleRange = angleRange;
        this.distanceRange = distanceRange;
    }
}

 

 这里虽然实现了相机的旋转及缩放,但是在旋转的过程中触发了UI的Click事件怎么处理呢。 后续再讲。 有空点个关注吧 。 相互学习 相互进步

 

你可能感兴趣的:(Unity)