- Home /
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.
If you guys have any tips on how I can achieve this I'd love to hear them.
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.
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.
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));
}
}
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
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.
this should work but it will give you a higher ray density towards the bottom. nice and simple solution
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
![](https://koobas.hobune.stream/wayback/20220612235957im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody.position causes shaking 1 Answer
Input problem 1 Answer
2.5D Shooting Rigidbodies vs RayCasts 0 Answers