Unity3D实现点击按钮控制摄像机移动到物体附近观察物体

一、创建场景,且添加一些物体,还有搭建按钮面板及其按钮

Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第1张图片
二、创建基础的控制摄像机移动脚本
/***
*	Title:"测试" 项目
*		主题:测试摄像机的视角切换
*	Description:
*		功能:XXX
*	Date:2017
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SimpleUIFrame
{
	public class Test_ViewChange : MonoBehaviour
	{
        public Camera MainCamera;                                               //主摄像机
        public Transform LookAtTarget;                                          //摄像机看向的目标
        public float CameraDistance = 6.0F;                                     //摄像机与看向目标的距离
        public float CameraHeight = 3.0F;                                       //摄像机高度
        public float CmaeraOffset = 1.0F;                                       //摄像机的偏移
        public float MainCameraMoveSpeed = 2F;                                  //主摄像机移动的速度
        public float waitTime=0F;                                               //等待摄像机移动到设备附近的时间
     

        private Vector3 LookAtTargetPosition;                                  //看向目标时的位置
        private Quaternion LookAtTargetRotation;                               //看向目标,且旋转

        public bool IsLookAtAppointTarget=false;                                //是否看向指定的物体
        public bool IsBack = false;

        private Vector3 _MainCameraOriginalPosition;                            //主摄像机的原始位置



        public IEnumerator  Start()
        {
            yield return new WaitForSeconds(0.1F);
            //记录主摄像机的原始位置
            if (MainCamera!=null)
            {
                _MainCameraOriginalPosition = new Vector3(MainCamera.transform.localPosition.x, MainCamera.transform.localPosition.y, MainCamera.transform.localPosition.z);
                print("主摄像机的原始位置的X="+_MainCameraOriginalPosition.x);
                print("主摄像机的原始位置的Y=" + _MainCameraOriginalPosition.y);
                print("主摄像机的原始位置的Z=" + _MainCameraOriginalPosition.z);
                print("主摄像机的原始位置的Z=" + _MainCameraOriginalPosition.z);
            }
        }
           

        private void FixedUpdate()
        {
            
            if (IsLookAtAppointTarget==true)
            {
                MainCamera.transform.position = Vector3.Lerp(MainCamera.transform.position, LookAtTargetPosition, Time.deltaTime * MainCameraMoveSpeed);
                MainCamera.transform.LookAt(LookAtTarget);
               
               // StartCoroutine(Stop(waitTime));
            }
            if (IsBack==true)
            {
                MainCamera.transform.position = Vector3.Lerp(MainCamera.transform.position, LookAtTargetPosition, Time.deltaTime * MainCameraMoveSpeed);
               
                //StartCoroutine(Stop(waitTime));
            }
           
        }

        /// 
        /// 摄像机看向指定物体的方法
        /// 
        public void LookAtAppointTarget()
        {
            
            if (LookAtTarget != null)
            {
                LookAtTargetPosition = new Vector3(LookAtTarget.transform.position.x + CmaeraOffset, LookAtTarget.transform.position.y + CameraHeight, LookAtTarget.transform.position.z + CameraDistance);
                IsLookAtAppointTarget = true;
                
            }
            else
            {
                Debug.LogError(GetType()+ "/LookAtAppointTarget()/看向的物体不存在,请检查!!!");
            }
            
           
        }

        //摄像机返回原始位置
        public void ReturnOriginalPosition()
        {
            LookAtTargetPosition = new Vector3(_MainCameraOriginalPosition.x, _MainCameraOriginalPosition.y, _MainCameraOriginalPosition.z);
            IsBack = true;
            
        }

        IEnumerator Stop(float waitTime)
        {
            yield return new WaitForSeconds(waitTime);
            if (IsLookAtAppointTarget==true)
            {
                IsLookAtAppointTarget = false;
                print("IsLookAtAppointTarget=" + IsLookAtAppointTarget);
            }
            if (IsBack==true)
            {
                IsBack = false;
                print("IsBack=" + IsBack);
            }
            
        }

        

    }//class_end
}

三、创建控制摄像机移动到物体附近的脚本
/***
*	Title:"XXX" 项目
*		主题:XXX
*	Description:
*		功能:XXX
*	Date:2017
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using kernal;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace SimpleUIFrame
{
	public class Test_SingleViewChange : Test_ViewChange
	{
        public Camera MainCamera1;
        public Transform sphere;
        public Transform cube;

        private Button BtnSphere;                                                //查看圆球按钮
        private Button BtnCube;                                                  //查看Cube按钮

        private void Awake()
        {
            //RigisterBtn1();
        }
        private void Start()
        {
           base.MainCamera = MainCamera1;
           StartCoroutine(base.Start());
        }

        
        //靠近圆球方法
        public void CloseShere()
        {
           
                base.LookAtTarget = sphere;
                base.LookAtAppointTarget();
                
        }

        //靠近Cube方法
        public void CloseCube()
        {
           
                base.LookAtTarget = cube;
                base.LookAtAppointTarget();
             
        }

        
    }
}

四、将 Test_SingleViewChange 脚本挂载到脚本管理器上,然后指定对应的主摄像机、需要移动到的物体
Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第2张图片
五、然后分别给对应的查看圆球、看向Cube、返回原位的按钮指定对应的方法
Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第3张图片
Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第4张图片
Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第5张图片
六、运行场景,然后分别点击 查看圆球、看向Cube、摄像机就会移动到对应的物体附近,点击返回原位的按钮则摄像机返回初始位置
Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第6张图片
Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第7张图片
Unity3D实现点击按钮控制摄像机移动到物体附近观察物体_第8张图片

你可能感兴趣的:(Unity基础,Unity3D,MoveMainCamera)