unity雷达交互

插件:TouchScript
1.相机挂脚本
在这里插入图片描述
2.场景中添加脚本
在这里插入图片描述
3.新增脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//需要引入这两个命名空间
using TouchScript;
using TouchScript.Pointers;

public class TuioListener : MonoBehaviour
{
    void Start()
    {
        TouchManager.Instance.PointersPressed += OnPointersPressed;
    }

    private void OnPointersPressed(object sender, PointerEventArgs e)
    {
        Debug.Log("[Test] OnPointersPressed");
        //打印所有触点信息
        IList<Pointer> pointers = e.Pointers;
        for (int i = 0; i < pointers.Count; i++)
        {
            Pointer p = pointers[i];
            Debug.LogFormat("Id={0}, Type={1}, Buttons={2}, Position={3}, PreviousPosition={4}, Flags={5}",
                p.Id, p.Type, p.Buttons, p.Position.ToString(), p.PreviousPosition.ToString(), p.Flags);
            //检测是否点中了3D物体
            Raycast(p.Position);
        }
    }

    private Vector3 origin, direction;
    private void Update()
    {
        //在场景中显示射线方便观察调式
        Debug.DrawRay(origin, direction, Color.red);
    }

    //发射射线
    private void Raycast(Vector2 screenPoint)
    {
        Ray ray = Camera.main.ScreenPointToRay(screenPoint);
        origin = ray.origin;
        direction = ray.direction;
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, int.MaxValue))
        {
            Debug.Log("检测到物体: " + hit.transform.name);
        }
    }
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐