1. 程式人生 > 其它 >Unity控制相機移動以及旋轉簡單操作,指令碼掛載相機上

Unity控制相機移動以及旋轉簡單操作,指令碼掛載相機上

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

public class CameraMovieManager : MonoBehaviour
{
    float _rotationX; 
    float rotationY; 
    public float rotsensityY = 5.0f;
    public float rotsensityX = 5.0f; 
    public float minimumVert = -45.0f; 
    public float maximumVert = 45.0f; //是否可以控制旋轉 public bool CanControl = false;

    float posX;
    float posY;

    public float PossensitivitX= 1.0f;
    public float PossensitivitY = 1.0f;
    // Update is called once per frame
    void Update()
    {
        CameraRot();
        CameraMove();
    }

    void CameraRot()
    {
        //點選滑鼠右鍵旋轉攝像頭
        if (Input.GetMouseButton(1))
        {
            rotationY = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * rotsensityY;
            _rotationX -= Input.GetAxis("Mouse Y") * rotsensityX;
            _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
            transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
        }

    }

    void CameraMove()
    {

        if (Input.GetMouseButton(0))
        {
            posX = transform.position.x + -Input.GetAxis("Mouse X") * PossensitivitX;
            posY = transform.position.y + -Input.GetAxis("Mouse Y") * PossensitivitY;
            transform.position = new Vector3(posX, posY, 0);
        }
    }
}