unity物体检测

OnTrigger和OnCollision
(1)OnCollision的触发条件
碰撞的物体至少一方加上刚体组件,且两个碰撞体都不要勾选IsTrigger,则双方都有碰撞消息
(最好用带有刚体的物体为运动体,仅带有碰撞体的物体为静态体)
(2)OnTrigger的触发条件
碰撞的物体至少一方加上刚体组件,至少一方勾选IsTrigger,则双方都有碰撞消息(最好用带有刚体的物体为运动体,仅带有碰撞体的物体为静态体)
射线检测3D
(1)原理
通过鼠标点击屏幕,由屏幕点,向Unity三维直接发射一条无限长的射线,当检测到碰撞物体后,会返回被碰撞物体的所有信息,以及交点信息等等….射线检测的牛逼之处在于,只要发出的射线与带有碰撞盒的物体,都会发生碰撞,并可以返回各种信息,例如被碰撞物体的位置、名称、法线 等等一系列的数据,另外可以 自定义发出射线的 距离、影响到的图层等等
(2)代码①
实现的效果:鼠标右键按地板的任何一个位置,小球就到那个位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayCast : MonoBehaviour {
    public Transform Ball; //小球(用来标记坐标)
    //设置射线在Plane上的目标点target
    private Vector3 target;
    void Update()
    {
        if (Input.GetMouseButton(1)) //点击鼠标右键
        {
            object ray = Camera.main.ScreenPointToRay(Input.mousePosition);//屏幕坐标转射线
            RaycastHit hit;//射线对象是:结构体类型(存储了相关信息)
            bool isHit = Physics.Raycast((Ray)ray, out hit);//发出射线检测到了碰撞   isHit返回的是 一个bool值
            if (isHit)
            {
                target = hit.point; //检测到碰撞,就把检测到的点记录下来
            }
        }
        //如果检测到小球的坐标 与 碰撞的点坐标 距离大于0.1f,就移动小球的位置到 碰撞的点 :target
        Ball.position = Vector3.Distance(Ball.position, target) > 0.1f ? Vector3.Lerp(Ball.position, target, Time.deltaTime) : target;
        //Move(target);//以上是Move函数的简写,此函数可不调用
    void Move(Vector3 target)
    {
        if (Vector3.Distance(Ball.position, target) > 0.1f)
        {
            Ball.position = Vector3.Lerp(Ball.position, target, Time.deltaTime);
        }
        //如果物体的位置和目标点的位置距离小于 0.1时直接等于目标点
        else
            Ball.position = target;
    }
(3)代码②
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Information : MonoBehaviour {
    private GameObject target;
    void Update()
    {
        if (Input.GetMouseButton(0)) //点击鼠标左键
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//屏幕坐标转射线
            RaycastHit hit;//碰撞信息对象结构体
            bool isRaycast = Physics.Raycast(ray, out hit);
            if (isRaycast)
            {
                Debug.DrawLine(ray.origin, hit.point, Color.red);
                target = hit.collider.gameObject;
                Debug.Log("名字" + target.name);
                Debug.Log("坐标" + hit.transform.position);
                Debug.Lo***" + hit.point);
                Debug.Log("重心坐标" + hit.barycentricCoordinate);
                Debug.Log("碰撞盒" + hit.collider);
                Debug.Log("距离" + hit.distance);
                Debug.Log("光线地图坐标" + hit.lightmapCoord);
                Debug.Log("法线" + hit.normal);
                Debug.Log("刚体" + hit.rigidbody);
                Debug.Log("纹理坐标" + hit.textureCoord);
                Debug.Log("三角指数" + hit.triangleIndex);//等等
            }
        }
    }
}
(5)实现的效果:当点击到物体,输出台那里输出射线碰撞到的物体的信息

射线检测2D
代码
private GameObject target;
void Update()
{
      if (Input.GetMouseButton(0))//点击鼠标左键
     {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);//碰撞信息对象结构体
            if (hit.collider != null)
            {
                     target = hit.collider.gameObject;
                     if (target.tag == "no")
                     {
                             Debug.Log(target.name); GameObject.Find(target.name).transform.Find("mask").gameObject.SetActive(true);
                     }
            }
     }
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务