注意:本文中每個功能都是分別使用cinemachine和camera兩種組件分別實作的,請按需提取,如果有問題可以直接下方留言,看到我會解答,另外加個關注你也不虧是不是?,本文2021.1.5榷訓再更新其他功能實作
一、多個攝像機平滑切換
當場景中存在多個虛擬相機時,關閉當前的主虛擬相機,就會平滑切換到所有開啟狀態中priority值比較高的相機,也就是說當關閉一個相機,并同時開啟一個相機時,鏡頭就會平滑平移過去,
FreeLocckCam、Camera實作
//先停再開,可以在權值相同且同時開啟的情況下切換相機
//FreeLocckCam、Camera都可以使用這種方式
camerObject.SetActive (false)
camerObject.SetActive (true)
在inspector中能設定切換速度和平滑曲線
二、重置 攝像機
重置攝像機包括重置位置、視角、視距
Camera組件實作
public void ResetCamera(){
// 重置視距
this.Camera.main.OrthographicSize = this.defaultViewScale;
this.Camera.main.FieldOfView = this.defaultViewScale;
// 重置位置
this.Camera.main.tansform.position = this.defaultPos;
this.Camera.main.tansform.rotation = this.defaultRot;
}
FreeLookCam組件實作
由于freelook 有自己的環顧坐標體系,所以重置時不止要重置攝像機,還要重置freelook組件的相關引數
public void ResetCamera(){
// 重置角度
//將binding mode改為LockToTargetWithWorldUp
freeLookCam.PreviousStateIsValid = false;
freeLookCam.m_XAxis.Value = 0;
freeLookCam.m_YAxis.Value = 0.5f;
//如果X軸位置有問題,請查看heading>bias值
// 重置視距
this.mainCineCamera.m_Lens.OrthographicSize = this.defaultViewScale;
this.mainCineCamera.m_Lens.FieldOfView = this.defaultViewScale;
// 重置位置
this.mainCineCamera.LookAt.tansform = this.LookAtDefaultPos;
this.mainCineCamera.Follow.transform = this.followDefalutPos;
}
三、旋轉視角
旋轉視角有兩種方式,一種是以攝像機自身為中心,環顧四周,另一種是以目標為中心,攝像機圍繞目標旋轉,
使用FreeLookCam 圍繞目標旋轉
首先在場景中添加好freelookCam組件,然后在inspector中設定follow物體和lookat物體,follow物體指的是相機圍繞物體旋轉時的原點,lookat指的是香鏡頭聚焦的位置,不管怎么旋轉,攝像機總會面對這個物體,
在inspector的 top、middle、buttom中可以設定這個旋轉范圍的高度和半徑
public float rotateAxisSpeed_x = 3f;
public float rotateAxisSpeed_y = 3f;
public void RotateAxis(float x, float y)
{
mainCineCamera.m_XAxis.m_InputAxisValue = x * rotateAxisSpeed_x;
mainCineCamera.m_YAxis.m_InputAxisValue = y * rotateAxisSpeed_y;
}
void Update(){
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
this.RotateAxis(h,v);
}
四、平移視角
所謂平移視角,就是在當前攝像機的視角下,對攝像機進行相對平移操作,
Camera組件實作
public float moveSpeedXY = 10f;
// 以前后移動速度
public float moveSpeedZ = 60f;
void Update()
{
// 獲取水平及滾輪按鍵值 區間為 0-1f
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
float mouse = Input.GetAxis("Mouse ScrollWheel");
//呼叫平移的方法
transform.Translate(new Vector3(h * moveSpeedXY, v * moveSpeedXY, mouse * moveSpeedZ) * Time.deltaTime, Space.Self);
}
FreeLocckCam組件實作
使用這個組件需要考慮兩種情況:
1 有跟隨無目標的情況
這個實作比較簡單,和上面用Camera組件實作的差不多,只需不過需要移動的不是攝像機本身,而是要移動 Follow的物體,
2 有跟隨有目標的情況
有跟隨目標代表環顧功能和平移功能需要共存,由于旋轉攝像機時相機始終面向目標,所以如果單純移動Follow物體,視角還是會依然頂著目標方向的,所以需要保證follow的物體和lookat的物體同時輕易,由于我們的需求是“根據相機視角平移”,那么為了避免產生死回圈,需要將這幾個物件分別建立跟蹤關系, 這個功能的實作也有很多種方式,下面介紹兩種
第一種實作方式:
進行平移時移動follow和lookat目標的父物體,在移動的同時關閉freecamera組件,移動結束后再打開,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraController: MonoBehaviour
{
// 上下左右的移動速度
public float moveSpeedXY = 10f;
// 以前后移動速度
public float moveSpeedZ = 60f;
public CinemachineFreeLook freeLook;
private bool isCameraMoving = false;
Vector3 defaultPos;
Vector3 defaultRot;
void Update()
{
// 獲取水平及滾輪按鍵值 區間為 0-1f
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
float mouse = Input.GetAxis("Mouse ScrollWheel");
if (h + v != 0f)
{
//呼叫平移的方法
if (!isCameraMoving)
{
// 獲取移動前的偏移
defaultPos = Camera.main.transform.position - transform.position;
defaultRot = Camera.main.transform.rotation.eulerAngles - transform.rotation.eulerAngles;
// 關閉組件避免死回圈
freeLook.gameObject.SetActive(false);
isCameraMoving = true;
}
// 平移鏡頭
Camera.main.transform.Translate(new Vector3(h * moveSpeedXY, v * moveSpeedXY, mouse * moveSpeedZ) * Time.deltaTime, Space.Self);
}
else
{
if (isCameraMoving)
{
// 重置跟隨和瞄準目標位置與主攝像機對齊
transform.position = Camera.main.transform.position - defaultPos;
transform.eulerAngles = Camera.main.transform.rotation.eulerAngles - defaultRot;
// 啟動freelook
freeLook.gameObject.SetActive(true);
isCameraMoving = false;
}
}
}
}
第二種方式(推薦):
讓lookat目標與相機的方向保持一致,然后讓相機的follow目標跟隨lookat目標,當平移時,只需移動lookat目標物體即可,比較推薦這種方式,簡單合理,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraController: MonoBehaviour
{
// 上下左右的移動速度
public float moveSpeedXY = 10f;
// 以前后移動速度
public float moveSpeedZ = 60f;
public CinemachineFreeLook freeLook;
void Update()
{
//
freeLook.LookAt.transform.eulerAngles = new Vector3(0f, Camera.main.transform.rotation.eulerAngles.y, 0f);
// 獲取水平及滾輪按鍵值 區間為 0-1f
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
float mouse = Input.GetAxis("Mouse ScrollWheel");
if (h + v != 0f)
{
freeLook.LookAt.transform.Translate(new Vector3(h * moveSpeedXY, mouse * moveSpeedZ, v * moveSpeedXY) * Time.deltaTime, Space.Self);
}
}
}
五、縮放視角
視角縮放是通過修改攝像機的FieldOfView和orthographic實作的,
Camera組件實作
private float currentViewScale;
public float scaleSpeed = 10.0f;
public float scaleMinScale = 1.0f;
public float scaleMaxScale = 150.0f;
void Start() {
// for camera view scale
this.currentViewScale = Camera.main.orthographic?Camera.main.OrthographicSize:Camera.main.FieldOfView;
}
void Update(){
this.currentViewScale += Input.GetAxis("Mouse ScrollWheel") * this.scaleSpeed;
this.currentViewScale = Mathf.Clamp(this.currentViewScale, this.minScale, this.maxScale);
if (Camera.main.orthographic)
{
Camera.main.OrthographicSize = this.currentViewScale;
}
else
{
Camera.main.FieldOfView = this.currentViewScale;
}
}
cinemachine組件實作
private float currentViewScale;
public float scaleSpeed = 10.0f;
public float scaleMinScale = 1.0f;
public float scaleMaxScale = 150.0f;
void Start() {
// for camera view scale
this.currentViewScale = Camera.main.orthographic? mainCineCamera.m_Lens.OrthographicSize:this.currentViewScale = mainCineCamera.m_Lens.FieldOfView;
}
void Update(){
this.currentViewScale += Input.GetAxis("Mouse ScrollWheel") * this.scaleSpeed;
this.currentViewScale = Mathf.Clamp(this.currentViewScale, this.minScale, this.maxScale);
if (Camera.main.orthographic == true)
{
this.mainCineCamera.m_Lens.OrthographicSize = this.currentViewScale;
}
else
{
this.mainCineCamera.m_Lens.FieldOfView = this.currentViewScale;
}
}
六、自動繞過障礙物
該功能只使用FreeLocckCam實作
- 虛擬相機需添加 cinemachine collider組件
- 在 cinemachine collider 設定作用層
- 在 cinemachine collider 可以設定鏡頭繞物的方式,距離,數量,和平滑度等等
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/248210.html
標籤:其他
下一篇:羅技游戲滑鼠介紹 (持續更新中)
