球形射线检测
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jiance : MonoBehaviour
{
public Transform guntr;
public GameObject ballYuan;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
int radius = 20;
Collider[] wts = Physics.OverlapSphere(guntr.transform.position, radius,1<< LayerMask.NameToLayer("Cube"));
if (wts.Length > 0)
{
for(int i = 0; i < wts.Length; i++)
{
Debug.Log("检测到物体" + wts[i].name);
if (wts[i].name != "fangyuta")
{
GameObject ball = Instantiate(ballYuan, guntr.position, guntr.rotation);
ball.GetComponent<Rigidbody>().AddForce(guntr.forward * 800f);
Destroy(ball, 1f);
}
}
}
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(this.transform.position, 20);
}
}