Search Unity

Raycast failing for any direction

Discussion in 'Scripting' started by kaleidox_, Jun 9, 2019.

  1. kaleidox_

    kaleidox_

    Joined:
    Jun 9, 2019
    Posts:
    19
    I have the following code, which is supposed to only delete cubes that are NOT directly in front of the player's view:

    Code (CSharp):
    1.  
    2.             RaycastHit hit;
    3.             Vector3 facing = new Vector3(player.transform.localRotation.x, player.transform.localRotation.y, player.transform.localRotation.z);
    4.  
    5.             if (Physics.Raycast(player.transform.position, facing, out hit, distance * 2)
    6.                 && hit.collider.gameObject.CompareTag("ApproachCube"))
    7.             {
    8.                 // was looking at block
    9.             }
    10.             else
    11.             {
    12.                 // was not looking at block
    13.                 Destroy(cube);
    14.             }
    The thing is, that ALL cubes, even the one I have EXACTLY in front of the player's view get Destroyed. What am i missing?
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hey, add just before your raycast
    Code (CSharp):
    1. Debug.DrawLine(player.transform.position, facing, distance * 2)
    With this you can see in game where your raycast is occuring and check if it visually matches what you want/expect.

    Then you might want to have a look at the "transform.forward" property to use as a direction for the raycast (your "facing" variable).

    When that part is right, try logging what your raycast is finding, could have the tag wrong or a frequent issue in such case is having another collider in the way. Logging what you're raycasting will let you know that.
     
  3. kaleidox_

    kaleidox_

    Joined:
    Jun 9, 2019
    Posts:
    19
    @_met44 thanks for the response!

    it looks like for some reason, my ray is being cast to the ground.

    I also fail to print ANY log information to the console. it just wont appear for some reason. (probably doing it wrong; how exactly do i log in the best way?)

    From the docks, i read that `transform.forward` returns the blue arrow direction? that is not what i need to have :thinking:

    EDIT: now with using `transform.forward`, the check is TRUE for all blocks, now..
     
  4. kaleidox_

    kaleidox_

    Joined:
    Jun 9, 2019
    Posts:
    19
    upload_2019-6-9_21-14-19.png

    this is what i am currently getting. the white line is the ray. but all of those blocks are triggering a successfull ray collision
     
  5. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    989
    Transform.localRotation is a quternion and the x,y,z values do not represent the usual Euler angles you're thinking of. You can either a) Use the built-in property to get the ruler angles from the quternion or b) simply use transform.forward to get the exact forward facing direction of the object. Personally, I'd go with the second choice.
     
  6. kaleidox_

    kaleidox_

    Joined:
    Jun 9, 2019
    Posts:
    19
    understood, thank you @Sluggy. although i am still having problems, as stated in my previous message
     
  7. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    989
    There isn't enough code for us to determine exactly what is going on. Are you able to post the entire function or class even? All we can tell from that example is that something called 'cube' is destroyed if the raycast hits nothing, otherwise, nothing happens.
     
  8. kaleidox_

    kaleidox_

    Joined:
    Jun 9, 2019
    Posts:
    19
    @Sluggy that is basically all the code.
    - It iterates over a list of cubes,
    - Then it is supposed to check if the player is looking at the cube
    - if the player is not looking at the cube, destroy it
    - if the player is looking at the cube, do nothing

    there isn't much more there to provide
     
  9. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    the gizmo you see in editor might show global coordinates, make sure it is set to local to see the actual direction of the selected object !
     
  10. kaleidox_

    kaleidox_

    Joined:
    Jun 9, 2019
    Posts:
    19
    This question has been answered in the Unity Discord channel. The problem was, that when iterating over all cubes, the ray will still always be the same, thus, will be true for all iterations.

    Closed