Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nijemi · Apr 27 at 11:06 PM · raycastmathraycasthitgeometryrays

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);
   }
 }





Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image elenzil · Apr 28 at 06:46 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

200 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to find the center of mass from capsulecast 0 Answers

Raycast always hitting the same place even if I click different areas? 2 Answers

Make camera hover at 50 over hilly terrain 2 Answers

How to Instantiate a line or a ray that goes through all colliders and send a message to them???? 1 Answer

Physics.Raycast issue | Ray not casting at certain angles? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges