unity---判断物体方位

点乘和叉乘#

image

画线#

image

GL#

image

源码#

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

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform tf;
    public float height =4f;
    public float lower = 10f;
    public GameObject Target;


    public Text x1,x2;


    void OnPostRender(){
        //   GL.LoadOrtho();
        GL.Begin(GL.LINES);

            GL.Color(Color.green); 
            GL.Vertex(Target.transform.position);
            GL.Vertex(tf.position);
        
        GL.End();
    }


    private void LateUpdate() {
             this.transform.position  = tf.position-Vector3.forward*lower+Vector3.up*height;
        this.transform.LookAt(tf);
      

        // 向量
        Vector3 target= Target.transform.position-tf.transform.position;
        Vector3 obj = tf.transform.forward;
        //计算
        if(Vector3.Dot(target,obj)>0)
        {
            x1.text="前面";
        }
        else x1.text="后面";

        if(Vector3.Cross(target,obj).y>0){
            x2.text="左边";
        }
        else x2.text="右边";

        //线段  点到点
        // Debug.DrawLine(Target.transform.position,tf.position,Color.red);
        //射线  初始点和方向,长度
        Debug.DrawRay(tf.position,tf.transform.forward*10f,Color.blue);

    }
    // Update is called once per frame
}

posted @   lxp_blog  阅读(688)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
主题色彩