本文实例为大家分享了Unity摄像机移至某物体附近观察的具体代码,供大家参考,具体内容如下
项目需求:要近距离观察上图的圆柱
解决核心:把摄像机移动到,圆柱前方,离圆柱z坐标5个单位的地方。
参考代码:此处移动用的是DOTween插件的“DOLocalMove(目标位置,耗费时间);”方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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 ; } } } |
实现效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_42935398/article/details/108379353