I’m trying to cast multiple rays from an object and I’m having trouble with the code.
I can easily make it work but the problem is that the code is just way too long I think, and I don’t know if ti can be done with just a simple function.
Here’s my code
RaycastHit hit;
Ray rayDown1 = new Ray(transform.position, Vector3.down);
Ray rayDown2 = new Ray(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 2), 0, 0), Vector3.down);
Debug.DrawRay(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, 0), Vector3.down, Color.blue);
Ray rayDown3 = new Ray(transform.position - new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, 0), Vector3.down);
Ray rayDown4 = new Ray(transform.position - new Vector3(0, 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
Ray rayDown5 = new Ray(transform.position + new Vector3(0, 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
Ray rayDown6 = new Ray(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
Ray rayDown7 = new Ray(transform.position - new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
Ray rayDown8 = new Ray(transform.position + new Vector3((this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, -(this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
Ray rayDown9 = new Ray(transform.position - new Vector3(-(this.gameObject.GetComponent<Renderer>().bounds.size.x / 4), 0, (this.gameObject.GetComponent<Renderer>().bounds.size.x / 4)), Vector3.down);
if (Physics.Raycast(rayDown1, out hit))
{
if (hit.collider.tag == "Boost")
{
PlateauScript script = hit.collider.GetComponent<PlateauScript>();
if (!script.wasUsed) GameHandler.Instance.PushForce = script.BoostAmount;
}
if (hit.collider.tag == "Checkpoint" && GameHandler.Instance.MovementSpeed != 0)
{
GameHandler.Instance.LastCheckpoint = hit.collider.gameObject;
GameHandler.Instance.CheckpointSpeed = GameHandler.Instance.MovementSpeed;
}
}
I’m trying to get all these rays to work with the if statement below there but I can’t find an easy way to do this without using a lot of code and a lot of if statements.