Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 MorbidJokes · Jun 03, 2020 at 11:07 AM · rigidbodyraycastraycastinggroundground detection

Using a range of raycasts with varying angles.

I've been trying to use Raycasts to scan a portion of a 3d sphere for ground detection but I can't seem to get the angle to work correctly (mainly because I'm just guessing at this point).

The picture below kind of shows what I'm trying to do, with the angle between the down direction and the highest raycast being adjustable. Except on a 3d Sphere.

alt text

If you guys have any tips on how I can achieve this I'd love to hear them.

capture.png (27.2 kB)
Comment
Add comment · Show 2
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 KoenigX3 · Jun 03, 2020 at 11:32 AM 1
Share

If it's for ground detection, wouldn't Physics.CheckSphere be more relevant? You need to define a position and a radius, and it returns true if it collides with anything.

avatar image MorbidJokes KoenigX3 · Jun 03, 2020 at 12:12 PM 0
Share

I thought about checkSphere and even tried to implement it at one point, but the functionality of this has gone beyond simply checking if the character is in contact with something and focuses on which rays are actually being triggered. I appreciate the suggestion though, thank you.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MorbidJokes · Jun 05, 2020 at 03:52 PM

I buckled down and figured something out that works while still giving me some pretty evenly spaced points.

The "angle" variable defines the maximum angle that the raycasts take, and the "resolution" is how many pieces that range is broken up into. so and angle of 45 and a resolution of 9 means the rays go in steps of 5 degrees.

It seems efficient enough for my use, but I did test it with 45 degree angles with a resolution of 45 and it just tanks, so there's probably some improvements I could make.

         float resolution = 9f;
         float angle = 45f;
         float radius = 0.5f;
 
         float Y, X, Z, tempAngle, tempAngle2;
         Vector3 direction;
         Vector3 center = transform.position;
 
         
         for(tempAngle = 0; tempAngle <= angle; tempAngle += angle / resolution)
         {
             Y = -Mathf.Cos(tempAngle * Mathf.Deg2Rad);
 
             for (tempAngle2 = 0; tempAngle2 <= 180; tempAngle2 += angle / resolution)
             {
                 X = Mathf.Cos(tempAngle2 * Mathf.Deg2Rad) * Mathf.Sqrt(1 - Mathf.Pow(Y, 2));
                 Z = Mathf.Sqrt(1 - Mathf.Pow(Y, 2) - Mathf.Pow(X, 2));
 
                 direction = Vector3.Normalize(new Vector3(X, Y, Z));
                 Debug.DrawRay(center, direction * (radius), new Color(X, Y, Z, 1));
 
                 direction = Vector3.Normalize(new Vector3(X, Y, -Z));
                 Debug.DrawRay(center, direction * (radius), new Color(X, Y, Z, 1));
             }
 
         }


Comment
Add comment · Share
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
0

Answer by michaelfelleisen · Jun 03, 2020 at 01:06 PM

https://forum.unity.com/threads/evenly-distributed-points-on-a-surface-of-a-sphere.26138/ this might help. just discard all points above a certain threshold

Comment
Add comment · Share
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
0

Answer by KoenigX3 · Jun 03, 2020 at 01:29 PM

I wrote a code, because I was interested in this topic.

 // These are the resolutions, odd numbers are favourable
 public int xCastNum = 35;
 public int zCastNum = 35;
 
 // Max angle (transform.down is 0° in this case)
 float maxAngle = 90;
 
 // Variables so you can use them later
 int minX;
 int maxX;
 
 int minZ;
 int maxZ;
 
 float angleMultiplierX;
 float angleMultiplierZ;
 
 private void Start()
 {
     RecalcX();
     RecalcZ();
 }
 
 private void Update()
 {
     for(int x = minX; x < maxX; x++)
     {
         for (int z = minZ; z < maxZ; z++)
         {
             Vector3 direction = Quaternion.AngleAxis(x * angleMultiplierX, transform.right) * Quaternion.AngleAxis(z * angleMultiplierZ, transform.forward) * -transform.up;
             Debug.DrawLine(transform.position, transform.position + direction);
         }
     }
 }
 
 void RecalcX()
 {
     minX = -xCastNum / 2;
     maxX = -minX + 1;
 
     angleMultiplierX = maxAngle / (xCastNum / 2);
 }
 
 void RecalcZ()
 {
     minZ = -zCastNum / 2;
     maxZ = -minZ + 1;
 
     angleMultiplierZ = maxAngle / (zCastNum / 2);
 }
 
 public void ChangeX(bool add)
 {
     if (add) xCastNum++;
     else xCastNum--;
     RecalcX();
 }
 
 public void ChangeZ(bool add)
 {
     if (add) zCastNum++;
     else zCastNum--;
     RecalcZ();
 }


I was not interested in even numbers, so if you need it, you have to implement it yourself. I added functions so you can link buttons to them and see how they work. I used Debug.Drawline to see the results.

If you want to make it rotation-independent, use Vector3.right and Vector3.forward.

Comment
Add comment · Show 2 · Share
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 michaelfelleisen · Jun 03, 2020 at 02:56 PM 0
Share

this should work but it will give you a higher ray density towards the bottom. nice and simple solution

avatar image MorbidJokes · Jun 04, 2020 at 08:01 PM 0
Share

This is so close to what I need thank you, but it distributes the rays in a square pattern when seen from above (tested with a max angle of 45).

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

219 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 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

Rigidbody.position causes shaking 1 Answer

Input problem 1 Answer

2.5D Shooting Rigidbodies vs RayCasts 0 Answers

remove forward component from velocity vector 2 Answers

Can't find a clone object with RayCast? [Solved] 2 Answers


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