unity實現用鼠標右鍵控制攝像機視角上下左右移動


using System;
using System.Collections.Generic;
using UnityEngine;
public class ViewControl
{
  enum RotationAxes
  {
    MouseXAndY,
    MouseX,
    MouseY
  }
  RotationAxes axes = RotationAxes.MouseXAndY;

  float sensitivityX = 10;
  float sensitivityY = 10;

  float minimumY = -45;
  float maximumY = 45;
  private float rotationY = 0;

  public void Update()
  {
    if (Input.GetMouseButton(0))
    {
      if (axes == RotationAxes.MouseXAndY)
      {
        float rotationX = Camera.main.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
        rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
        Camera.main.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
      }
      else if (axes == RotationAxes.MouseX)
      {
        Camera.main.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
      }
      else
      {
        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
        rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
        Camera.main.transform.localEulerAngles = new Vector3(-rotationY, Camera.main.transform.localEulerAngles.y, 0);
      }
    }
  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM