通过射线来检测物体并使用示例,这个是通过摄像机来发射射线的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sheixian : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray;
RaycastHit hit;
//1.创建射线
//2.射线检测并反馈结果
//鼠标点击一个东西,然后反馈给我们物体信息
if (Input.GetMouseButtonDown(0))
{
//把摄像机屏幕点转化为线 获取鼠标坐标
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//创建射线
if (Physics.Raycast(ray, out hit))//第一个参数是射线,第二个是碰撞的物体
{
Debug.Log(hit.collider.name);//碰到的物体信息
GameObject.Destroy(hit.collider.gameObject);//消除碰到的物体
}
}
}
}