Unity Community |

Hello guys, I am improving my moving and crush platforms in my 2.5D game, for this I was needing one way to make a wider raycast. How I can't do this, I did the following function:
Code:
function multiRayCast( startPt : Vector3, dir : Vector3, hit : RaycastHit, distance : float ) : boolean { var iterations : int = 5; // Number of raycasts to be created inside this angle var angleToCheck : float = 45.0; // This angle var anglePartition : float = angleToCheck/iterations; //Starting from one extreme of the angle // Rotating the raycast for( /*nothing*/; iterations > 0; iterations-- ) { { return true; } //Rotating until rotate angleToCheck degrees } return false; }
Just look the image and become easier to understand it, it just cast various raycasts in the direction given to check if there is some hit inside of that magenta area. The problem is, the function returns true but the "hit" parameter in the caller function is empty. Anyone have some idea to solve this? Thanks
My First Project, ArrowHead, a 2.5D Platform game: http://forum.unity3d.com/threads/130...-Platform-game
The code shown doesn't show how you check that the 'hit' is null, so my first question would be: Are you sure you're checking the correct 'hit'?`
I think you may get better performance with OverlapSphere and then checking gameobject tags.
Well I am considering that the hit is returning an empty RayCastHit because when I try to access any data from it the unity crashes.
Code:
If I use the commented normal raycast, it works okay. Also i am not using this Sphere because I can't control well like I can do with this function the parameters.
My First Project, ArrowHead, a 2.5D Platform game: http://forum.unity3d.com/threads/130...-Platform-game
I think your problem is that you are not referencing hit as an out variable. It is easily done in C#, but I cant see any documentation on how to do this in JS.
This returns 0
Unity 101 - Programming in General
Unity 202 - Know your objects
Unity 303 - Object Relations and Components
Unity 404 - Advanced Classes
Unity 505 - Networking (Bringing it all together)
If you don't know the stuff where you think you are, go back and learn the other stuff first.
@BigMisterB that is true, I read somewhere that the RayCastHit is a struct, not a reference for something, so you can't assign null for this one. Now I am curious, how the Raycast function works and change it if this is not a pointer.
I was just copying the idea from this function when I made my multiRayCast function. I'll try to check some values from this RayCastHit to check if there is something that say that it's empty.
My First Project, ArrowHead, a 2.5D Platform game: http://forum.unity3d.com/threads/130...-Platform-game
You could just as easily use an external hit
Code:
{ var iterations : int = 5; // Number of raycasts to be created inside this angle var angleToCheck : float = 45.0; // This angle var anglePartition : float = angleToCheck/iterations; //Starting from one extreme of the angle // Rotating the raycast for( /*nothing*/; iterations > 0; iterations-- ) { { return true; } //Rotating until rotate angleToCheck degrees } return false; }
Unity 101 - Programming in General
Unity 202 - Know your objects
Unity 303 - Object Relations and Components
Unity 404 - Advanced Classes
Unity 505 - Networking (Bringing it all together)
If you don't know the stuff where you think you are, go back and learn the other stuff first.
btw, for performance reasons, you may want to spread the raycasts over several frames... usually not a good idea to do more than a raycast or two per frame as they are expensive
Ops, sorry for the bump post, but thank you @BigMisterB, that idea of the global RayCastHIt worked well, and @Diablo now I am adaptiing my moving platform to use less raycasts ;P
My First Project, ArrowHead, a 2.5D Platform game: http://forum.unity3d.com/threads/130...-Platform-game