旋转相关

沿轴顺时针方向为正方向

摄像机自转:

方法:采用欧拉角限制角度

特点:摄像机沿X、Y轴的运动方向和鼠标方向相同

using UnityEngine;
using System.Collections;

/// <summary>
/// 按下鼠标右键,摄像机自转 ;沿着x轴旋转,与Y轴的夹角会变化/// </summary>
public class DoRotation : MonoBehaviour
{
    public float rotateSpeed = 10;
    /// <summary>沿X轴的旋转范围 </summary>
    public float yAxisMinAngle = 60;
    public float yAxisMaxAngle = 330;
     
    private void FixedUpdate()
    {
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");
        if (Input.GetMouseButton(1) && (x != 0 || y != 0))
        { 
            RotateView(x, y); 
        }
    }
    /// <summary> 用欧拉角限制X轴范围:大于330(抬头30度内)     小于60 (低头60度内)</summary>
    private void RotateView(float x, float y)
    {
        x *= rotateSpeed;
        y *= rotateSpeed;
        //1.y的移动方向和旋转方向相反,故要x轴+(-y);2.欧拉角没有负值
        if (this.transform.eulerAngles.x - y > yAxisMaxAngle || this.transform.eulerAngles.x - y < yAxisMinAngle)
        { 
            this.transform.Rotate(0, x, 0, Space.World);  //鼠标在屏幕坐标系的X轴有输入,绕世界坐标系的Y轴旋转x度
            this.transform.Rotate(-y, 0, 0);  //鼠标在屏幕坐标系的Y轴有输入,绕本地自身坐标系的x轴旋转-y度
        }
    }  
}
View Code

 摄像机围绕旋转:

方法:采用夹角限制角度

特点:摄像机沿X、Y轴的运动方向和鼠标方向相反

using UnityEngine;
using System.Collections;

/// <summary> 按下鼠标左键,摄像机围绕目标旋转/// </summary>
public class DoRotateAround : MonoBehaviour
{
    //当前围绕的目标
    public Transform targetTf;
    public float rotateSpeed = 10;
    /// <summary>与y轴最大夹角/// </summary>
    public float yMaxAngle = 160;
    /// <summary>与y轴最小夹角</summary>
    public float yMinAngle = 120;

    private void Start()
    {
        if (targetTf == null)
            targetTf = new GameObject("Target").transform;

        LookAtTarget();
    }

    private void FixedUpdate()
    {
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");
        if (Input.GetMouseButton(0) && (x != 0 || y != 0))
            RotateAround(x, y);
    }
    //注视
    private void LookAtTarget()
    {
        this.transform.LookAt(targetTf);
    }
    //注视旋转
    private void RotateAround(float x, float y)
    {
        this.transform.RotateAround
            (targetTf.position, Vector3.up, x * rotateSpeed);  //Vector3.up为世界坐标系的Y轴

        LimitAxisY(ref y);  //限制X轴旋转范围

        this.transform.RotateAround
            (targetTf.position, this.transform.right, -y);
    }
    //限制当前摄像机Z轴与世界Y轴夹角
    private void LimitAxisY(ref float y)
    {
        y *= rotateSpeed;
        float angle = Vector3.Angle(this.transform.forward, Vector3.up);
        if (angle < yMinAngle && y > 0 || angle > yMaxAngle && y < 0)
            y = 0; 
    }



}
View Code

 摄像机镜头缩放: 

using UnityEngine;
using System.Collections;

/// <summary>滑动鼠标滚轮镜头缩放/// </summary>
public class DoZoom  : MonoBehaviour
{
    public Camera targetCamera;
    public float scrollSpeed = 5;
    public float limitMax, limitMin = 1;
    public float interval = 10;//与地面间距

    private void Start()
    {
        if (targetCamera == null)
            targetCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();

        limitMax = Vector3.Distance(this.transform.position, Vector3.zero);
    }  
    private void FixedUpdate()
    {
        float offset = Input.GetAxis("Mouse ScrollWheel");
         
        if (offset != 0)
        {
            Zoom(offset  * scrollSpeed);
        }  
    }
    private void Zoom(float offset)
    {
        offset = LimitDistance(offset);
        this.transform.Translate(0, 0, offset);  
    }
    //限制缩放距离
    private float LimitDistance(float offset)
    {
        float distance = Vector3.Distance(this.transform.position, Vector3.zero);
        //如果间距小于最小距离并且向前移动  或者 间距大于最大距离并且向后移动
        if (distance < limitMin && offset > 0 || distance > limitMax && offset < 0)
            offset = 0;

        if (this.transform.position.y < interval && offset > 0)
            offset = 0;

        return offset;
    }
}
View Code

 物体自转:

方法:采用欧拉角限制角度

特点:物体沿X、Y轴的运动方向和鼠标方向相同

using UnityEngine;
using System.Collections;

/// <summary>
/// 按下鼠标右键,物体旋转 </summary>
public class DoRotation : MonoBehaviour
{
    public float rotateSpeed = 10;
    /// <summary>沿X轴的旋转范围 </summary>
    public float yAxisMinAngle = 10;
    public float yAxisMaxAngle = 330;

    private void FixedUpdate()
    {
        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");
        if (Input.GetMouseButton(1) && (x != 0 || y != 0))
        {
            RotateView(x, y);
        }
    }
    /// <summary> 用欧拉角限制X轴范围:大于330(抬头10度内)     小于60 (低头30度内)</summary>
    private void RotateView(float x, float y)
    {
        x *= rotateSpeed;
        y *= rotateSpeed;
        //1.y的移动方向和旋转方向相反,故要x轴+(-y);2.欧拉角没有负值
        if (this.transform.eulerAngles.x + y > yAxisMaxAngle || this.transform.eulerAngles.x + y < yAxisMinAngle)
        {
            this.transform.Rotate(0, -x, 0, Space.World);  //鼠标在屏幕坐标系的X轴有输入,绕世界坐标系的Y轴旋转x度
            this.transform.Rotate(y, 0, 0);  //鼠标在屏幕坐标系的Y轴有输入,绕本地自身坐标系的x轴旋转-y度
        }
    }
}
View Code

 

posted @ 2016-10-21 12:41  沐风先生  阅读(161)  评论(0编辑  收藏  举报