Unity/c#鼠标右键控制相机围绕物体旋转/滚轮控制远近

鼠标右键控制相机围绕物体旋转/滚轮控制远近

#c#代码`
鼠标右键控制相机围绕物体旋转/滚轮控制远近

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveModel : MonoBehaviour
{
    
    
    public Camera cam;//相机
    public float spee = 10;//速度
    float x = 0, y = 0;
    public float m_camFieldOfViewMin;
    public float m_camFieldOfViewMax;
    private void Awake()
    {
    
    
        m_camFieldOfViewMin = 15;
        m_camFieldOfViewMax = 100;
    }
    void move()
    {
    
    
        
        x= Input.GetAxis("Mouse X") * spee;
        y += Input.GetAxis("Mouse Y") * spee;//定义在x,y轴上移动
        cam.transform.RotateAround(transform.position, Vector3.up, x);
        cam.transform.localEulerAngles = new Vector3(-y, cam.transform.localEulerAngles.y, 0);//相机移动(旋转角度)
    }
    // Update is called once per frame
    void Update()
    {
    
    
        if (Input.GetMouseButton(1))
        {
    
    
            move();
        }
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
    
    
            if (cam.fieldOfView < m_camFieldOfViewMax)
            {
    
    
                cam.fieldOfView += 2;
            }
            if (cam.orthographicSize <= 10)
            {
    
    
                cam.orthographicSize += 1f;
            }
        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
    
    
            if (cam.fieldOfView > m_camFieldOfViewMin)
            {
    
    
                cam.fieldOfView -= 2;
            }
            if (cam.orthographicSize >= 1)
            {
    
    
                cam.orthographicSize -= 1f;
            }
        }
    }
}


代码给物体,把相机拖到cam
仅分享,不商用,错误地方劳烦指点,侵删
发现新问题:发现在模型一个方向旋转180度后另一个方向的控制会出问题,本来鼠标向左模型向左旋转,向上或向下旋转180度后,鼠标向左模型向右
改善方法
代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveModel : MonoBehaviour
{
    
    
	public Camera cam;
    public float spee = 5;
    public float speedrotate = 50;
    float x = 0, y = 0;
    public float m_camFieldOfViewMin;
    public float m_camFieldOfViewMax;
    
    private void Awake()
    {
    
    
        m_camFieldOfViewMin = 15;
        m_camFieldOfViewMax = 100;
       
    }
    void Start()
    {
    
    
       
    }
    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);
    }
    void move()
    {
    
    
        float fMouseX = Input.GetAxis("Mouse X");
        float fMouseY = Input.GetAxis("Mouse Y");
        transform.Rotate(Vector3.up, -fMouseX * spee, Space.World);
        transform.Rotate(Vector3.right, fMouseY * spee, Space.World);

    }
    // Update is called once per frame
    void Update()
    {
    
    
        if (Input.GetMouseButton(1))
        {
    
    
            move();
        }
       
        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
    
    
            if (cam.fieldOfView < m_camFieldOfViewMax)
            {
    
    
                cam.fieldOfView += 2;
            }
            if (cam.orthographicSize <= 10)
            {
    
    
                cam.orthographicSize += 1f;
            }
        }
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
    
    
            if (cam.fieldOfView > m_camFieldOfViewMin)
            {
    
    
                cam.fieldOfView -= 2;
            }
            if (cam.orthographicSize >= 1)
            {
    
    
                cam.orthographicSize -= 1f;
            }
        }
    }
}

同样代码给物体,把相机拖到cam

猜你喜欢

转载自blog.csdn.net/m0_53934771/article/details/123110722
今日推荐