本文实例为大家分享了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;
 }
 }
}

实现效果:

Unity摄像机移至某物体附近观察此物体

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。