Unity物体围绕中中心旋转加角度

Unity物体围绕中中心旋转加角度


```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 视角切换
/// 
public class PerspectiveSwitch : MonoBehaviour
{
    public Camera m_Camera;

    public Transform rotationPoint;         //围绕旋转的中心点

    public List SwitchPositonList;
    [SerializeField]
    private float rotateSpeed = 5f;         //旋转的速度

    int index = 1;


    void Update()
    {
        InputKeySwitch();

        RotateView();
    }

    /// 
    /// 键盘控制视角的远近
    /// 
    void InputKeySwitch()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            if (index == SwitchPositonList.Count)
            {
                index = 0;
            }

            m_Camera.fieldOfView = SwitchPositonList[index];
            index++;
        }
    }

    /// 
    /// 视角旋转
    /// 
    void RotateView()
    {
        var mouse_x = Input.GetAxis("Mouse X");
        var mouse_y = -Input.GetAxis("Mouse Y");
        if (Input.GetKey(KeyCode.Mouse1))
        {
            transform.RotateAround(rotationPoint.position, Vector3.up, mouse_x * rotateSpeed);
            
            //限制上下角度
            {
                //预设角度(当前角度加上将要增加/减少的角度)   
                float rotatedAngle = transform.eulerAngles.x + mouse_y * rotateSpeed;
                if (rotatedAngle < 45)
                {
                    transform.RotateAround(rotationPoint.position, transform.right, (mouse_y * rotateSpeed) + (45 - rotatedAngle));
                }
                else if (rotatedAngle > 70)
                {
                    transform.RotateAround(rotationPoint.position, transform.right, 0);
                }
                else
                {
                    transform.RotateAround(rotationPoint.position, transform.right, mouse_y * rotateSpeed);
                }
            }
        }

        
    }


}

你可能感兴趣的:(Unity,unity3d,c#)