Search Unity

Array of Rays in Unity?

Discussion in 'Scripting' started by Jadefire16, Oct 17, 2019.

  1. Jadefire16

    Jadefire16

    Joined:
    Jul 14, 2019
    Posts:
    19
    So I'm trying to create a line of sight for a project i'm working on in my college course, I've been trying to raycast an array of rays from a cubes face however I can't seem to find a method that unity will accept

    This is what I have so far:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RayTrace : MonoBehaviour
    6. {
    7.    static readonly float MaxRayDistance = 100;
    8.  
    9.     public int numOfRays;
    10.  
    11.  
    12.     public Ray[] rays;
    13.  
    14.    public LayerMask mask;
    15.     //Creates a layermask to tell the raycast what to interact with
    16.  
    17.     // Update is called once per frame
    18.  
    19.     private void Start()
    20.     {
    21.         Ray[] rays = new Ray[numOfRays](transform.position, transform.forward);
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.  
    27.  
    28.         // Ray[] rays = new Ray[](transform.position, transform.forward);
    29.         foreach (Ray item in rays)
    30.         {
    31.             RaycastHit hit;
    32.  
    33.             if (Physics.Raycast(item, out hit, MaxRayDistance, mask))
    34.             {
    35.                 Debug.Log(hit.collider);
    36.                 Debug.DrawRay(item.origin, hit.point, Color.red);
    37.             }
    38.             else { Debug.DrawLine(item.origin, item.origin + item.direction * MaxRayDistance, Color.blue); }
    39.         }
    40.  
    41.     }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What's wrong with that? Does it throw an error? Does it cause undesired behavior? If so, what do you expect to happen and what actually happens? Please include more information.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It looks like you are imagining that saying new Ray[] is going to create a whole bunch of Rays. Actually, that just creates the array itself. You then need to set rays[0] = new Ray(something), rays[1] = new Ray(something), etc.
     
    Antypodish and Jadefire16 like this.
  4. Jadefire16

    Jadefire16

    Joined:
    Jul 14, 2019
    Posts:
    19
    Thank you, this seems to have fixed my problem, much appreciated!
     
  5. Jadefire16

    Jadefire16

    Joined:
    Jul 14, 2019
    Posts:
    19
    My bad, It seems like you cant use Foreach with the Ray class, Still looking for a way to create multiple rays to form a Line of SIght
     
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You can't say "foreach thing in this Ray" because Ray is not a collection. But you can say "foreach Ray in this collection of Rays". The type of thing you store in a collection has no impact on whether or not you can do a foreach.

    Guessing you made some different mistake somewhere.
     
  7. xXDrago04Xx

    xXDrago04Xx

    Joined:
    Apr 26, 2021
    Posts:
    1
    hey did you ever work this out can you send me the code?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Instead of hijacking a two-year-old thread with a dubious resolution (to me it sounds like nothing was resolved above, that there was a fundamental language misunderstanding on the part of the OP), if you want your problem solved, then start your own thread, it's FREE.

    When you post, if you want even the slightest chance of a useful response, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/