2

У меня есть куб, и мне просто надо сделать, что бы он пустил лучи во все стороны. Лучи в вверх, вниз, влево, право я уже сделал. А вот как пустить их под углом? Можете помочь? кубик и лучи

1 ответ 1

5
public class Foo : MonoBehaviour
{
    [SerializeField] private int _rayCount = 16;
    [SerializeField] private LayerMask _mask = 1 << 0; // Default layer
    [SerializeField] private float _distance = Mathf.Infinity;

    private void Start ()
    {
        RaycastHit2D[] hits = GetAroundHits2D(transform.position, _rayCount, _mask, _distance);
    }

    public RaycastHit2D[] GetAroundHits2D (Vector2 origin, int rayCount, LayerMask mask, float distance = Mathf.Infinity)
    {
        float radianDelta = Mathf.PI*2f/(float)rayCount;
        var hits = new RaycastHit2D[rayCount];
        for (int i = 0; i < rayCount; i++) 
        {
            float radian = i*radianDelta;
            Vector2 direction = new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
            hits[i] = Physics2D.Raycast(origin, direction, distance, mask);
        }
        return hits;
    }
}

Но если вам нужно пускать лучи во все стороны, скорее всего вы занимаетесь какой-то фигней. Лучше опишите проблему которую вы пытаетесь решить столь варварским способом.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.