请选择 进入手机版 | 继续访问电脑版
MSIPO技术圈 首页 IT技术 查看内容

unity相机视角平移一段距离

2023-07-13

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;
}

 

 

相关阅读

热门文章

    手机版|MSIPO技术圈 皖ICP备19022944号-2

    Copyright © 2024, msipo.com

    返回顶部