unity相机视角平移一段距离

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

public class ControlCamera : MonoBehaviour
{


    //相机视角焦点平移一段距离
    //需求  相机视角   内的3D对象A    在视角内平移到屏幕C点   
    public Vector3 InitCameraPos;
    // Start is called before the first frame update
    void Start()
    {
        InitCameraPos = Camera.main.transform.position;
    }
    public Transform A;
    public Transform B;
    public Transform UI;
    public bool OpenA;
    public Vector3 Span;//相机坐标加上这个插值
    // Update is called once per frame
    void Update()
    {


        if (OpenA)
        {


            //从相机 发送一条射线到UI  撞击到Plane  会发现 撞击点的球和 UI  始终在屏幕上同一个点
            RaycastHit hitInfoUI;//平行于摄像机Z轴向量的射线
            if (Physics.Raycast(Camera.main.transform.position, UI.position - Camera.main.transform.position, out hitInfoUI))
            {
                UIWorldPositionTra.position = hitInfoUI.point;
            }



            RaycastHit hitInfoA;//平行于摄像机Z轴向量的射线  
            if (Physics.Raycast(Camera.main.transform.position, UI.position - Camera.main.transform.position, out hitInfoUI) &&
                Physics.Raycast(A.position, A.position + Camera.main.transform.forward * 10000, out hitInfoA))
            {
                Span = hitInfoA.point - hitInfoUI.point ;
#if UNITY_EDITOR
                Debug.DrawLine(hitInfoA.point, hitInfoUI.point, Color.red);      
#endif
            }





#if UNITY_EDITOR
            //Debug.DrawLine(Camera.main.transform.position, A.position, Color.red);
            //Debug.DrawLine(A.position, A.position + Camera.main.transform.forward * 10000, Color.red);

            Debug.DrawLine(A.position, A.position + Camera.main.transform.forward * 10000, Color.red);
            Debug.DrawLine(UI.position, UI.position + Camera.main.transform.forward * 10000, Color.blue);


            Debug.DrawLine(Camera.main.transform.position, Camera.main.transform.position + (UI.position - Camera.main.transform.position) * 1000, Color.black);
#endif
            return;
        }

    }
    public Transform UIWorldPositionTra;
}

unity相机视角平移一段距离_第1张图片

unity相机视角平移一段距离_第2张图片

 unity相机视角平移一段距离_第3张图片

 

你可能感兴趣的:(unity,数码相机,游戏引擎)