- Home /
How to get Raycasts in all directions(Vector3)?
Hey Unity pros :)
I have this Raycast here:
var LookAtTarget : Transform; var rayRange = 10.0;
function Update () { if (Physics.Raycast(transform.position, transform.forward, rayRange)) { transform.LookAt(LookAtTarget); } }
It works great, but only in the z-direction(transform.forward), but i would like to have this ray casted in every direction x,y and z,. But i am not cableable of doing so. I have tried to insert Vector3 like so:
var LookAtTarget : Transform; var rayRange = 10.0;
function Update () { if (Physics.Raycast(transform.position, Vector3, rayRange)) { transform.LookAt(LookAtTarget); } }
But with no luck.
So can you please help me here? I want my ray to be casted in ALL directions.
Thanks.
please give some context to your script as i can see no point raycasting in every direction it will just give everything in your scene and if you try and look at it you wont be able to. are you looking from a point or do you want to click on something and look at it
Answer by titan68 · Sep 18, 2013 at 09:32 AM
Thanks, ZoomDomain. I've write it on C#.
private const int DIRECTIONS_COUNT = 16;
private const int MAX_DISTANCE = 10;
public float GetDistanceToGround()
{
var distances = new List<float>();
foreach (var direction in GetSphereDirections(DIRECTIONS_COUNT))
{
Debug.DrawRay(transform.position, direction, Color.blue);
RaycastHit groundHitInfo;
if (Physics.Raycast(transform.position, direction, out groundHitInfo, MAX_DISTANCE))
{
distances.Add(groundHitInfo.distance);
}
}
return distances.Any() ? distances.Min() : MAX_DISTANCE;
}
private Vector3[] GetSphereDirections(int numDirections)
{
var pts = new Vector3[numDirections];
var inc = Math.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;
}
Answer by Bravini · Jan 20, 2011 at 05:22 PM
it's a simple fix, you need to use a SphereCast instead of raycast mate http://unity3d.com/support/documentation/ScriptReference/Physics.SphereCast.html or SphereCastAll if you want to check more than one target (an explosion for example).
Good Luck!
SpehereCast is just like a raycast, only it's a "tube". It's not the same thing as scaling a sphere; it's translating it.
Sorry, I may just be incompetent, but I still dont get it. The docs in this case is not very usefull. Or I may not just be cableable of understanding it.
SphereCast, not "SpehereCast", sorry. Imagine you shoot a BB gun, with an infinitely tiny BB that's a raycast. Shooting a big ball out of a cannon - that's a spherecast. But it's still in one direction, even though it's got a radius. And its radius is fixed. What the OP is asking for, is akin to taking an infinitely tiny spherical balloon, and seeing if it touches anything before it is blown up to a certain size. Ins$$anonymous$$d, with CheckSphere, we start with the balloon blown up all the way to that size, and see if anything is inside it. Same result.
Answer by MountDoomTeam · Dec 01, 2012 at 10:06 AM
you couldnt cast a ray in all directions, as that would be infinite... you could cast it in 10000 directions, or 1000 000, using one of these phi or a disco ball formulas: http://www.softimageblog.com/archives/115 see the action script formula in the middle.
depending on the size of possible targets and their distance, you could calculate the number of racasts needed to see all of them... sphere casting is useful if they hide behind walls.
Answer by umair_hassan · Aug 26, 2016 at 07:52 AM
how can i debug ray then, like, if i had to debug ray in single direction, i do: if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out hit, 5)) { Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow); }
But I can't able to debug ray on each direction? Kindly help me in this regard.
you can go for a for-loop with drawing all rays first (maybe gather them in an array if needed)
Your answer
Follow this Question
Related Questions
iphone touch, only working in one scene 1 Answer
Setting the position of linerenderer 1 Answer
raycast and instantiat advice 0 Answers
problem with scripting 1 Answer