
项目需求:要近距离观察上图的圆柱
解决核心:把摄像机移动到,圆柱前方,离圆柱z坐标5个单位的地方。
参考代码:此处移动用的是DOTween插件的“DOLocalMove(目标位置,耗费时间);”方法。
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using DG.Tweening; 5 6 7public class TestCameraMove : MonoBehaviour 8{ 9 private Vector3 cameraOriginPos = new Vector3(0,1,-10); 10 public Transform targetTra; 11 //private Vector3 aimPos = targetGOPos - new Vector3(); 12 public bool flag; 13 void Awake() 14 { 15 16 } 17 18 void Start() 19 { 20 21 } 22 23 void Update() 24 { 25 26 } 27 public void MoveCamera() 28 { 29 Vector3 aimPos = targetTra.localPosition - new Vector3(0,0,5); 30 if (!flag) 31 { 32 Camera.main.transform.DOLocalMove(aimPos,2); 33 flag = true; 34 } 35 else 36 { 37 Camera.main.transform.DOLocalMove(cameraOriginPos,2); 38 flag = false; 39 } 40 } 41}
实现效果:
