unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
更新時(shí)間:2021年04月12日 15:10:12 作者:SuperWiwi
這篇文章主要介紹了unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
在Update函數(shù)中執(zhí)行:
if (turnAround)
{
playerCamera.RotateAround(transform.localPosition, transform.up, Time.deltaTime * rotateSpeed);
degree += Time.deltaTime * rotateSpeed;
if (degree >= 360)
{
turnAround = false;
degree = 0;
}
}
補(bǔ)充:unity 攝像機(jī)圍繞某個(gè)物體進(jìn)行旋轉(zhuǎn)放大縮小
腳本通過以一個(gè)物體為中心點(diǎn)
來控制攝像機(jī)圍繞物體旋轉(zhuǎn)縮放 ,腳本掛在攝像機(jī)上即可,target是需要觀看的物體
public Transform Camera2;
public GameObject target;
private float dis;
public float xSpeed = 200, ySpeed = 200, mSpeed = 10; //移動(dòng)速度
public float yMinLimit = -50, yMaxLimit = 50; //攝像機(jī)的Y軸移動(dòng)最小最大限制
public float distance = 7, minDistance = 2, maxDistance = 30; //攝像機(jī)與目標(biāo)物體的距離
public bool needDamping = true; //阻尼默認(rèn)開啟
float damping = 5.0f; //默認(rèn)阻尼為5.0F
public float x = 0.0f; //X軸
public float y = 0.0f; //Y軸
// Use this for initialization
void Start() {
instance = this;
Camr = Camera2Rotation.Close;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
private void Update()
{
}
void LateUpdate()
{
//使用按下鼠標(biāo)左鍵移動(dòng)物體
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * disVector + target.transform.position;
//adjust the camera
if (needDamping)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
}
else
{
transform.rotation = rotation;
transform.position = position;
}
}
/// <summary>
/// 旋轉(zhuǎn)角度的控制
/// </summary>
/// <param name="angle">旋轉(zhuǎn)的角度</param>
/// <param name="min">最小角度</param>
/// <param name="max">最大角度</param>
/// <returns></returns>
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
void MoveCameraToTarget()
{
if (target != null)
{
Camera2.LookAt(target.transform);
Camera2.position = Vector3.MoveTowards(Camera2.position, target.transform.position, 5 * Time.deltaTime);
dis = Vector3.Distance(Camera2.transform.position, target.transform.position);
if (dis < 1.5f)
{
Camr = Camera2Rotation.Open;
CancelInvoke();
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:
- Unity3D實(shí)現(xiàn)播放gif圖功能
- Unity 如何獲取鼠標(biāo)停留位置下的物體
- Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作
- 解決unity rotate旋轉(zhuǎn)物體 限制物體旋轉(zhuǎn)角度的大坑
- unity AudioSource播放完聲音后要執(zhí)行的函數(shù)或條件操作
- Unity3d 使用Gizmos畫一個(gè)圓圈
- unity 如何使用LineRenderer 動(dòng)態(tài)劃線
- Unity 通過LineRenderer繪制兩點(diǎn)之間的直線操作
- Unity 實(shí)現(xiàn)給物體替換材質(zhì)球
- Unity解析gif動(dòng)態(tài)圖操作
相關(guān)文章
C#使用SendMessage實(shí)現(xiàn)進(jìn)程間通信的方法
這篇文章主要介紹了C#使用SendMessage實(shí)現(xiàn)進(jìn)程間通信的方法,涉及C#中SendMessage方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#時(shí)間格式轉(zhuǎn)換為時(shí)間戳的方法步驟
這篇文章主要介紹了C#時(shí)間格式轉(zhuǎn)換為時(shí)間戳的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
C#使用Socket進(jìn)行簡(jiǎn)單的通訊的示例代碼
Socket 類是基于與 Linux、macOS 或 Windows 的本機(jī)互操作性提供的托管代碼版本的套接字服務(wù),提供了一系列的接口來支持應(yīng)用層的調(diào)用,下面我們就來學(xué)習(xí)一下如何使用Socket進(jìn)行簡(jiǎn)單的通訊,需要的可以參考下2023-12-12
C# 批量生成隨機(jī)密碼必須包含數(shù)字和字母并用加密算法加密
這篇文章主要介紹了C# 批量生成隨機(jī)密碼必須包含數(shù)字和字母并用加密算法加密,需要的朋友參考下2017-01-01

