1.按下鼠標(biāo)右鍵可以實現(xiàn)攝像機上下左右旋轉(zhuǎn)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotate : MonoBehaviour
{
//旋轉(zhuǎn)速度
public float rotationSpeed = 5f;
//上下旋轉(zhuǎn)角度限制
public float maxVerticalAngle = 90f;
public float minVerticalAngle = -90f;
//旋轉(zhuǎn)緩沖速度
public float lerpSpeed = 10f;
private float targetRotationX = 0f;
private float targetRotationY = 0f;
void Update()
{
if (Input.GetMouseButton(1))
{
// 獲取鼠標(biāo)輸入的旋轉(zhuǎn)增量
float rotationXInput = -Input.GetAxis("Mouse Y");
float rotationYInput = Input.GetAxis("Mouse X");
// 根據(jù)旋轉(zhuǎn)速度進行攝像機的旋轉(zhuǎn)
targetRotationX += rotationXInput * rotationSpeed;
targetRotationY += rotationYInput * rotationSpeed;
// 對上下旋轉(zhuǎn)角度進行限制
targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);
// 根據(jù)旋轉(zhuǎn)角度更新攝像機的歐拉角,Quaternion.Lerp可以使攝像機旋轉(zhuǎn)更加平滑
Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
}
}
}
2.按下鼠標(biāo)右鍵可以實現(xiàn)攝像機圍繞某個物體上下左右旋轉(zhuǎn)文章來源:http://www.zghlxwxcb.cn/news/detail-642563.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotate1 : MonoBehaviour
{
public Transform target;
public float rotationSpeed = 5f;
public float maxVerticalAngle = 90f;
public float minVerticalAngle = -90f;
public float lerpSpeed = 200f;
public float distance = 10;
private float targetRotationX = 0f;
private float targetRotationY = 0f;
void Start()
{
if (target == null)
Debug.LogError("Please assign a target to the orbit camera!");
}
void Update()
{
if (Input.GetMouseButton(1))
{
float rotationXInput = -Input.GetAxis("Mouse Y");
float rotationYInput = Input.GetAxis("Mouse X");
targetRotationX += rotationXInput * rotationSpeed;
targetRotationY += rotationYInput * rotationSpeed;
targetRotationX = Mathf.Clamp(targetRotationX, minVerticalAngle, maxVerticalAngle);
Quaternion targetRotation = Quaternion.Euler(targetRotationX, targetRotationY, 0f);
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, lerpSpeed * Time.deltaTime);
}
transform.position = target.position - transform.forward * distance;
}
}
3.攝像頭始終跟隨在某個物體的正后方文章來源地址http://www.zghlxwxcb.cn/news/detail-642563.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotate2 : MonoBehaviour
{
public Transform target;
public float followSpeed = 2f;
public float followHeight = 4f;
public float distance = 8f;
private Vector3 velocity = Vector3.zero;
void Start()
{
if (target == null)
Debug.LogError("Please assign a target to the orbit camera!");
}
void LateUpdate()
{
Vector3 targetPosition = target.position - (target.forward * distance)+new Vector3(0,followHeight,0);
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, 1f / followSpeed);
transform.LookAt(target);
}
}
到了這里,關(guān)于Unity三種攝像機旋轉(zhuǎn)方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!