Unity攝像機移至某物體附近觀察此物體
更新時間:2020年09月05日 09:11:48 作者:Unity李大饞師
這篇文章主要為大家詳細介紹了Unity攝像機移至某物體附近,觀察此物體,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity攝像機移至某物體附近觀察的具體代碼,供大家參考,具體內容如下

項目需求:要近距離觀察上圖的圓柱
解決核心:把攝像機移動到,圓柱前方,離圓柱z坐標5個單位的地方。
參考代碼:此處移動用的是DOTween插件的“DOLocalMove(目標位置,耗費時間);”方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class TestCameraMove : MonoBehaviour
{
private Vector3 cameraOriginPos = new Vector3(0,1,-10);
public Transform targetTra;
//private Vector3 aimPos = targetGOPos - new Vector3();
public bool flag;
void Awake()
{
}
void Start()
{
}
void Update()
{
}
public void MoveCamera()
{
Vector3 aimPos = targetTra.localPosition - new Vector3(0,0,5);
if (!flag)
{
Camera.main.transform.DOLocalMove(aimPos,2);
flag = true;
}
else
{
Camera.main.transform.DOLocalMove(cameraOriginPos,2);
flag = false;
}
}
}
實現(xiàn)效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

