Как определить несколько объектов в сфере?
Я пытаюсь реализовать датчик RADAR в единстве. В настоящее время я использую сферу для определения объекта и измерения его скорости и расстояния. На данный момент, хотя в радиусе сотворенной сферы есть несколько объектов, он определяет только ближайший. Как я могу заставить его распознать все объекты внутри сферы?
Буду признателен за любую помощь. Это то, что я сделал сейчас, который просто идентифицирует ближайший объект.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spherecast : MonoBehaviour
{
Rigidbody rb;
public GameObject curobject;
public float radius;
public float maxdist;
public LayerMask layermask;
public float velocity;
public Time deltatime;
public Vector3 previous;
private Vector3 origin;
private Vector3 direction;
private float hitdist;
// Use this for initialization
void Start()
{
previous = curobject.transform.position;
}
// Update is called once per frame
void Update()
{
velocity = hitdist/Time.deltaTime;
origin = transform.position;
direction = transform.forward;
RaycastHit hit;
if (Physics.SphereCast(origin, radius, direction, out hit, maxdist, layermask, QueryTriggerInteraction.UseGlobal))
{
curobject = hit.transform.gameObject;
hitdist = hit.distance;
Debug.Log("Distance" + hitdist);
velocity = ((curobject.transform.position - previous).magnitude) / Time.deltaTime;
previous = curobject.transform.position;
Debug.Log("Velocity" + velocity);
// Debug.Log("Velocity = " + velocity);
}
else
{
hitdist = maxdist;
curobject = null;
}
}
private void ondrawgizmosselected()
{
Gizmos.color = Color.red;
Debug.DrawLine(origin, origin + direction * hitdist);
Gizmos.DrawWireSphere(origin + direction * hitdist, radius);
}
}
1 ответ
Как я могу заставить его распознать все объекты внутри сферы?
Я считаю, что вам нужно использовать SphereCastAll вместо SphereCast для этого.
https://docs.unity3d.com/ScriptReference/Physics.SphereCastAll.html
Ответ:
"Как и Physics.SphereCast, но эта функция будет возвращать все попадания, которые пересекает развертка сферы".
Возвращает RaycastHit[].