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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class RayTrace : MonoBehaviour { static readonly float MaxRayDistance = 100; public int numOfRays; public Ray[] rays; public LayerMask mask; //Creates a layermask to tell the raycast what to interact with // Update is called once per frame private void Start() { Ray[] rays = new Ray[numOfRays](transform.position, transform.forward); } void Update() { // Ray[] rays = new Ray[](transform.position, transform.forward); foreach (Ray item in rays) { RaycastHit hit; if (Physics.Raycast(item, out hit, MaxRayDistance, mask)) { Debug.Log(hit.collider); Debug.DrawRay(item.origin, hit.point, Color.red); } else { Debug.DrawLine(item.origin, item.origin + item.direction * MaxRayDistance, Color.blue); } } }
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.
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.
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
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.
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/