项目需求:要近距离观察上图的圆柱 解决核心:把摄像机移动到,圆柱前方,离圆柱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; } } }实现效果: