本文中的功能是unity 使用 camera 組件 和 cinemachine camera組件分別實作的,還未寫完,有空繼續更新
多個攝像機平滑切換
使用組件:FreeLocckCam
當場景中存在多個虛擬相機時,關閉當前的主虛擬相機,就會平滑切換到所有開啟狀態中priority值比較高的相機,也就是說當關閉一個相機,并同時開啟一個相機時,鏡頭就會平滑平移過去,
//先停再開,可以在權值相同且同時開啟的情況下切換相機
camerObject.SetActive (false)
camerObject.SetActive (true)
在inspector中能設定切換速度和平滑曲線
重置 鏡頭視角
使用組件:FreeLocckCam
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;
}
使用組件: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;
}
旋轉視角
使用組件:FreeLocckCam
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;
}
平移視角
使用組件: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 有跟隨無目標的情況
這個實作比較簡單,只需要設定 Follow物體的平移位置即可,
2 有跟隨有目標的情況
有跟隨目標代表環顧功能和平移功能需要共存,由于相機移動時始終面向目標,所以平移時不會是鏡頭角度平移,所以需要先保證follow的物體與camera的rotation保持方向一致才行,注意以下實作的效果是,平移程序結束才能環顧,是一個比較笨的方法
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目標與相機的rotation保持一致,然后讓相機的follow目標跟隨lookat目標,當平移時,只需移動lookat目標物體即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class ViewControl : 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);
}
}
}
縮放視角
使用組件: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/248166.html
標籤:其他
