Search Unity

Raycasts hit value void

Discussion in 'Scripting' started by McC1oud, Aug 28, 2015.

  1. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    I designed my collisions by sending out a raycast straight down and gathering the hit point value to compare with the transform of the object. The issue I am running into is that if the ray cast hit fails to hit a target, it's output becomes 0 which when compared to the object transform would then set the gameobject to 0 in world space.

    The fix I came up with is:

    if raycast == false
    groundValue = - 1000;

    This works but setting the value like that seems wrong. Any techniques for this scenario?

    ---Edit

    This has lead to other issues.
    Reworking.
     
    Last edited: Aug 28, 2015
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Posting the code would make it alot easier to help, but from what you have said i think doing something like this will help:

    Code (CSharp):
    1. If(Physics.Raycast(origin, dir, out hit, dist)){
    2. //your hit code in here
    3. }
     
  3. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    I understand how to raycast, the issue is that I am comparing the players transform location with the hit point data to detect the ground collision. If no hit is detected, the comparison value becomes 0, so the player transform will stop when it hits Y value of 0 in world space.
     
  4. McC1oud

    McC1oud

    Joined:
    Mar 14, 2015
    Posts:
    96
    Here is the script after a few fixes, now works as I want it but that value still bugs me.

    preCal i use to precalculate the position of the transform on the next frame. If the preCal goes below the value of the hitpoint value then it will snap the transform down to the surface(this is to fix the animation problem of snapping up on the following frame if I compared it to the current value).

    Line 7 is the issue


    Code (CSharp):
    1.         castGround ();
    2.      
    3.         groundVal = floorHit.point.y;
    4.  
    5.         if(gCast == false)
    6.         {
    7.             groundVal = -10000.0f;
    8.             gravityToggle = true;
    9.         }
    10.      
    11.         if(gravityToggle == true)
    12.         {          
    13.             gravityVal = trueGravity;
    14.             applyGravity();
    15.          
    16.         }
    17.      
    18.  
    19.         if (pre_CalFrame.y < groundVal)
    20.         {
    21.             transform.position = new Vector3(transform.position.x, groundVal, transform.position.z);
    22.             gravityToggle = false;
    23.         }
    24.  
    25.         if (pre_CalFrame.y > groundVal)
    26.         {
    27.             gravityToggle = true;
    28.         }