方法一:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamerMoveTest2 : MonoBehaviour {
public Transform target;//獲取旋轉(zhuǎn)目標(biāo)
public float speed = 0.5f;
private void Update()
{
if (Input.GetMouseButton(0))
{
camerarotate();
}
camerazoom();
}
private void camerarotate() //攝像機(jī)圍繞目標(biāo)旋轉(zhuǎn)操作
{
transform.RotateAround(target.position, Vector3.up, speed*Time.deltaTime); //攝像機(jī)圍繞目標(biāo)旋轉(zhuǎn)
var mouse_x = Input.GetAxis("Mouse X");//獲取鼠標(biāo)X軸移動(dòng)
var mouse_y = -Input.GetAxis("Mouse Y");//獲取鼠標(biāo)Y軸移動(dòng)
if (Input.GetKey(KeyCode.Mouse1))
{
transform.Translate(Vector3.left*(mouse_x*15f)*Time.deltaTime);
transform.Translate(Vector3.up*(mouse_y*15f)*Time.deltaTime);
}
if (Input.GetKey(KeyCode.Mouse0))
{
transform.RotateAround(target.transform.position, Vector3.up, mouse_x*5);
transform.RotateAround(target.transform.position, transform.right, mouse_y*5);
}
}
private void camerazoom() //攝像機(jī)滾輪縮放
{
if (Input.GetAxis("Mouse ScrollWheel") > 0)
transform.Translate(Vector3.forward*0.5f);
if (Input.GetAxis("Mouse ScrollWheel") < 0)
transform.Translate(Vector3.forward*-0.5f);
}
}
方法二:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
//攝像機(jī)距離
public float distance = 10.0f;
//縮放系數(shù)
public float scaleFactor = 1f;
public float maxDistance = 30f;
public float minDistance = 5f;
//記錄上一次手機(jī)觸摸位置判斷用戶是在左放大還是縮小手勢(shì)
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private Vector2 lastSingleTouchPosition;
private Vector3 m_CameraOffset;
private Camera m_Camera;
public bool useMouse = true;
//定義攝像機(jī)可以活動(dòng)的范圍
public float xMin = -100;
public float xMax = 100;
public float zMin = -100;
public float zMax = 100;
//這個(gè)變量用來記錄單指雙指的變換
private bool m_IsSingleFinger;
//初始化游戲信息設(shè)置
void Start()
{
m_Camera = this.GetComponent<Camera>();
m_CameraOffset = m_Camera.transform.position;
}
void Update()
{
//判斷觸摸數(shù)量為單點(diǎn)觸摸
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Began || !m_IsSingleFinger)
{
//在開始觸摸或者從兩字手指放開回來的時(shí)候記錄一下觸摸的位置
lastSingleTouchPosition = Input.GetTouch(0).position;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
MoveCamera(Input.GetTouch(0).position);
}
m_IsSingleFinger = true;
}
else if (Input.touchCount > 1)
{
//當(dāng)從單指觸摸進(jìn)入多指觸摸的時(shí)候,記錄一下觸摸的位置
//保證計(jì)算縮放都是從兩指手指觸碰開始的
if (m_IsSingleFinger)
{
oldPosition1 = Input.GetTouch(0).position;
oldPosition2 = Input.GetTouch(1).position;
}
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
ScaleCamera();
}
m_IsSingleFinger = false;
}
//用鼠標(biāo)的
if (useMouse)
{
distance -= Input.GetAxis("Mouse ScrollWheel") * scaleFactor;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
if (Input.GetMouseButtonDown(0))
{
lastSingleTouchPosition = Input.mousePosition;
Debug.Log("GetMouseButtonDown:" + lastSingleTouchPosition);
}
if (Input.GetMouseButton(0))
{
MoveCamera(Input.mousePosition);
}
}
}
/// <summary>
/// 觸摸縮放攝像頭
/// </summary>
private void ScaleCamera()
{
//計(jì)算出當(dāng)前兩點(diǎn)觸摸點(diǎn)的位置
var tempPosition1 = Input.GetTouch(0).position;
var tempPosition2 = Input.GetTouch(1).position;
float currentTouchDistance = Vector3.Distance(tempPosition1, tempPosition2);
float lastTouchDistance = Vector3.Distance(oldPosition1, oldPosition2);
//計(jì)算上次和這次雙指觸摸之間的距離差距
//然后去更改攝像機(jī)的距離
distance -= (currentTouchDistance - lastTouchDistance) * scaleFactor * Time.deltaTime;
//把距離限制住在min和max之間
distance = Mathf.Clamp(distance, minDistance, maxDistance);
//備份上一次觸摸點(diǎn)的位置,用于對(duì)比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
//Update方法一旦調(diào)用結(jié)束以后進(jìn)入這里算出重置攝像機(jī)的位置
private void LateUpdate()
{
var position = m_CameraOffset + m_Camera.transform.forward * -distance;
m_Camera.transform.position = position;
}
private void MoveCamera(Vector3 scenePos)
{
Vector3 lastTouchPostion = m_Camera.ScreenToWorldPoint(new Vector3(lastSingleTouchPosition.x, lastSingleTouchPosition.y, -1));
Vector3 currentTouchPosition = m_Camera.ScreenToWorldPoint(new Vector3(scenePos.x, scenePos.y, -1));
Vector3 v = currentTouchPosition - lastTouchPostion;
m_CameraOffset += new Vector3(v.x, 0, v.z) * m_Camera.transform.position.y;
//把攝像機(jī)的位置控制在范圍內(nèi)
m_CameraOffset = new Vector3(Mathf.Clamp(m_CameraOffset.x, xMin, xMax), m_CameraOffset.y, Mathf.Clamp(m_CameraOffset.z, zMin, zMax));
//Debug.Log(lastTouchPostion + "|" + currentTouchPosition + "|" + v);
lastSingleTouchPosition = scenePos;
}
}