Unity Community


Results 1 to 8 of 8

  1. Location
    Brazil
    Posts
    228

    Multi RayCast function can't return the RayCastHit object

    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:  
    1. function multiRayCast( startPt : Vector3, dir : Vector3, hit : RaycastHit, distance : float ) : boolean
    2. {
    3.     var iterations : int = 5; // Number of raycasts to be created inside this angle
    4.     var angleToCheck : float = 45.0; // This angle
    5.     var anglePartition : float = angleToCheck/iterations;
    6.    
    7.     //Starting from one extreme of the angle
    8.     var currDir : Vector3 = Quaternion.Euler( 0, 0, -1*anglePartition*(iterations/2) )*dir;
    9.    
    10.     // Rotating the raycast
    11.     for( /*nothing*/; iterations > 0; iterations-- )
    12.     {
    13.         Debug.DrawLine ( startPt , startPt + currDir*distance, Color.magenta );
    14.         if( Physics.Raycast( startPt, currDir, hit, distance ) )
    15.         {
    16.             Debug.Log("true");
    17.             return true;
    18.         }
    19.         //Rotating until rotate angleToCheck degrees
    20.         currDir = Quaternion.Euler( 0, 0, anglePartition )*currDir;
    21.     }
    22.     return false;
    23. }



    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

    My Second Project, RGBoom, a Platform Game too: http://forum.unity3d.com/threads/232...-platform-game


  2. Location
    Denmark
    Posts
    423
    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.
    My First Game - Looking for Feedback

    A game is a series of interesting choices.
    - Sid Meier


  3. Location
    Brazil
    Posts
    228
    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:  
    1.     //if( Physics.Raycast( playerGlobalPosition, movingDirection, rayCastHit, crushDetectDistance ) )
    2.     if( multiRayCast( playerGlobalPosition, movingDirection, rayCastHit, crushDetectDistance ) )
    3.     {
    4.         Debug.Log( "raycast" + rayCastHit.transform.gameObject.name );
    5.     }

    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

    My Second Project, RGBoom, a Platform Game too: http://forum.unity3d.com/threads/232...-platform-game


  4. Location
    Tennessee, USA
    Posts
    3,449
    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

    Code:  
    1. #pragma strict
    2.  
    3. function Start () {
    4.     var x : int = 0;
    5.     if(test(x))
    6.     print(x);
    7. }
    8.  
    9. function test(a : int) : boolean{
    10.     a = 5;
    11.     return true;
    12. }
    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.


  5. Location
    Brazil
    Posts
    228
    @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

    My Second Project, RGBoom, a Platform Game too: http://forum.unity3d.com/threads/232...-platform-game


  6. Location
    Tennessee, USA
    Posts
    3,449
    You could just as easily use an external hit

    Code:  
    1.  
    2. function multiRayCast( startPt : Vector3, dir : Vector3, distance : float ) : boolean
    3. {
    4.     var iterations : int = 5; // Number of raycasts to be created inside this angle
    5.     var angleToCheck : float = 45.0; // This angle
    6.     var anglePartition : float = angleToCheck/iterations;
    7.    
    8.     //Starting from one extreme of the angle
    9.     var currDir : Vector3 = Quaternion.Euler( 0, 0, -1*anglePartition*(iterations/2) )*dir;
    10.    
    11.     // Rotating the raycast
    12.     for( /*nothing*/; iterations > 0; iterations-- )
    13.     {
    14.         Debug.DrawLine ( startPt , startPt + currDir*distance, Color.magenta );
    15.         if( Physics.Raycast( startPt, currDir, hit, distance ) )
    16.         {
    17.             Debug.Log("true");
    18.             return true;
    19.         }
    20.         //Rotating until rotate angleToCheck degrees
    21.         currDir = Quaternion.Euler( 0, 0, anglePartition )*currDir;
    22.     }
    23.     return false;
    24. }
    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.


  7. Posts
    736
    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


  8. Location
    Brazil
    Posts
    228
    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

    My Second Project, RGBoom, a Platform Game too: http://forum.unity3d.com/threads/232...-platform-game

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •