92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
/*******************************************************************************
|
|
*Create By CG
|
|
*Function 摄像机控制
|
|
*******************************************************************************/
|
|
namespace ZXK.UTility
|
|
{
|
|
public class CameraMyControl : MonoBehaviour
|
|
{
|
|
[SerializeField]//镜头位移速度
|
|
private float _moveSpeed = 10.0f;
|
|
[SerializeField]//镜头旋转速度
|
|
private float _rotSpeed = 100.0f;
|
|
[SerializeField]//镜头拉近速度
|
|
private float _zoomSpeed = 100.0f;
|
|
//加速度倍数
|
|
private float _expedite = 5.0f;
|
|
|
|
private Camera _camera = null;
|
|
|
|
private float _cameraView = 60.0f;
|
|
|
|
private void Awake()
|
|
{
|
|
_camera = transform.GetComponentInChildren<Camera>();
|
|
if (_camera)
|
|
{
|
|
_cameraView = _camera.fieldOfView;
|
|
_camera.transform.localPosition = Vector3.zero;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) return;
|
|
//获取移动按键
|
|
Vector3 translation = GetInputTranslationDirection();
|
|
//加速
|
|
if (Input.GetKey(KeyCode.LeftShift))
|
|
{
|
|
//原速度*10为按下Shift后的速度
|
|
translation *= _expedite;
|
|
}
|
|
//移动
|
|
transform.Translate(translation * Time.deltaTime * _moveSpeed);
|
|
|
|
//旋转
|
|
if (Input.GetMouseButton(1))
|
|
{
|
|
Vector2 mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")*-1);
|
|
transform.Rotate(new Vector3(0, mouseMovement.x, 0) * Time.deltaTime * _rotSpeed,Space.World);
|
|
_camera.transform.Rotate(new Vector3(mouseMovement.y, 0, 0) * Time.deltaTime * _rotSpeed);
|
|
}
|
|
|
|
//摄像机拉近
|
|
_camera.transform.Translate(new Vector3(0, 0, Input.mouseScrollDelta.y * Time.deltaTime * _zoomSpeed));
|
|
}
|
|
|
|
Vector3 GetInputTranslationDirection()
|
|
{
|
|
Vector3 direction = new Vector3();
|
|
if (Input.GetKey(KeyCode.W))
|
|
{
|
|
direction += Vector3.forward;
|
|
}
|
|
if (Input.GetKey(KeyCode.S))
|
|
{
|
|
direction += Vector3.back;
|
|
}
|
|
if (Input.GetKey(KeyCode.A))
|
|
{
|
|
direction += Vector3.left;
|
|
}
|
|
if (Input.GetKey(KeyCode.D))
|
|
{
|
|
direction += Vector3.right;
|
|
}
|
|
if (Input.GetKey(KeyCode.Q))
|
|
{
|
|
direction += Vector3.up;
|
|
}
|
|
if (Input.GetKey(KeyCode.E))
|
|
{
|
|
direction += Vector3.down;
|
|
}
|
|
return direction;
|
|
}
|
|
}
|
|
}
|