- Home /
How to shoot many raycasts il all directions except the ground ?
Hi !
I've been trying to reproduce a adaptive echo system for my game based on this demo from Rory Walker I found : https://youtu.be/9vmtm7ed3Z8?t=441. What I am trying to do is to get an average value of the distance between the player and the sum of all of the hit points.
Being a newbie in programming, I tried my best to get an effective system by searching on various forums how to shoot raycasts in all directions except the floor.
I managed to get my system to work but the problem is that in the code I copied, the raycasts are shot in all directions including the floor. The reason why I need the rays to only hit what is above a certain vertical angle is because my player is a third person character and my listener (and my script) are attached to the camera, which distance from the ground can vary and then affect the average distance I'm passing to fmod, which could lead to some non realistic behaviour where the calculated average distances could be nearly the same in a big open field and in a closed room in function of how high is placed the camera.
Also I am totally incompetent in everything including math and geometry, and I guess that the solution in my code reside in my GetDirections() function, but unfortunately, I don't have the knowledge to adapt those calculations to my needs.
Thank you for your time !
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class RS_AudioEcho : MonoBehaviour
{
private RS_Camera m_camera = null;
private const int m_directionCount = 16;
private const int m_maxDistance = 500;
private int m_facticeSandlayerMask = 1 << 24;
private float m_averageDistance;
private static int m_rayCount = -1;
private static float m_distancesTotalValue = 0.0f;
private static float m_distancesAverageValue = 0.0f;
private void Awake()
{
m_camera = FindObjectOfType<RS_Camera>();
}
public float GetDistanceToGround()
{
List<float> distances = new List<float>();
m_rayCount = -1;
m_distancesTotalValue = 0.0f;
foreach (var direction in GetDirections(m_directionCount))
{
RaycastHit hitInfo;
if (Physics.Raycast(transform.position, direction, out hitInfo, m_maxDistance, m_facticeSandlayerMask))
{
distances.Add(hitInfo.distance);
Debug.DrawRay(transform.position, direction, Color.green);
m_rayCount ++;
m_distancesTotalValue = m_distancesTotalValue + hitInfo.distance;
}
}
return distances.Any() ? distances.Min() : m_maxDistance;
distances.Clear();
}
private Vector3[] GetDirections(int numDirections)
{
var pts = new Vector3[numDirections];
var inc = Mathf.PI * (3 - Math.Sqrt(5));
var off = 2f / numDirections;
foreach (var k in Enumerable.Range(0, numDirections))
{
var y = k * off - 1 + (off / 2);
var r = Math.Sqrt(1 - y * y);
var phi = k * inc;
var x = (float)(Math.Cos(phi) * r);
var z = (float)(Math.Sin(phi) * r);
pts[k] = new Vector3(x, y, z);
}
return pts;
}
void Update()
{
m_distancesAverageValue = m_distancesTotalValue / m_rayCount;
GetDistanceToGround();
// Debug.Log(m_rayCount);
// Debug.Log(m_distancesTotalValue);
//Debug.Log(m_distancesAverageValue);
}
}
Well, if "towards the floor" means heading down at all, just change your GetDirections() function so that y = abs(y). .. Assuming that "down" is negative "Y" axis.
Your answer
Follow this Question
Related Questions
How to find the center of mass from capsulecast 0 Answers
Raycast hitting objects to the left of my player 1 Answer
Raycast hits everything on the screen 2 Answers
Another Raycast Issue. 0 Answers