Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Raycasting from Camera to Player object not projecting correctly?

Discussion in 'General Discussion' started by Tempy111, Jan 27, 2022.

  1. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    I'm trying to do something which seams alot of people try and have some problems with but no replies i find seam to line up with my issues..

    I'm using RaycastAll to try to cast rays from the Camera to the player Gameobject, so any objects (with a set tag) between the player and the camera (thus blocking the users view, the code then sets that object to being transparent (or semi-transparent, i'll deal with that bit later).

    I'm using DrayRay option so i can see where the ray(s) are going but.. that seams to say the ray changes direction with the player object's facing direction, and the position on the camera it traces to is at the edge of the screen, the percise location varying with the direction of the player object.

    the camera isn't static but always faces the same direction giving a bit of an isometric appearance.

    The code for this bit is:
    Code (CSharp):
    1.  RaycastHit[] hits;
    2.         hits = Physics.RaycastAll(Camera.main.transform.position, Camera.main.transform.forward, DistanceToPlayer);
    3.         Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward, Color.green);
    I read someplace that raycast only works on objects with a collider, but RaycastAll seams to claim (in the unity documents) it'll work against anything) but still i only seam to get collisions with objects by the player object when it's facing in the opposite direction to the object.
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,996
    This is true.
    This is false, it doesn't say it anywhere here. All RayCasts work on the same way. No collider, no raycast hit.
    To be honest, I can't tell what's the problem. You have point A (you chose the camera transform position for some reason), and you have point B (camera forward X length) and then you get some hits along the way. So what do you expect exactly? What's your goal?

    Maybe you want to cast through the middle of the screen? If yes, this can help with that.
     
    neginfinity likes this.
  3. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    Ah, I mis-read bit on Raycast all (which is probebly why i said 'seams to claim', in that it says 'returns all hits' where as Raycast says about returning true if intersects with a collider.. So i mistook 'All hits' to mean the inclusive 'all' more then 'all, but with previous restrictions stated in earlier page'. totally my mistake in mis-reading that.

    i should have posted a screenshot to better explain, and sorry about that.. been.. well, doesn't matter ^_^


    anyway, here is a screen shot with the gizmo's on, the sound logo on the right is the player, (the left is an npc so that doens't matter) and as you can see, there is a tree between the player and the camera. I was trying to do a check to see if the player is blocked from camera view like that, make the blocking object (in this case a tree, checked via a tag) semi-transparent so it's not blocking the view. (bit annoying, i can't get the ray visible in this for this image since when i'm standing still, the ray doesn't display with any length.. don't quite understand how the drawray works)

    From looking around, I was basically told that doing a raytrace from the camera to the player would do the job and to use the camera's transform position as that's how you get the location of the camera.. Personally, I probebly also made a mistake thinking that the ray was a point, so the camera position registration point would be either bottom left (0x0 point) or the centre of view. where it seams it doesn't have a ray which is a point but the size of the camera view..

    Hope that helps explain it a bit better. Thanks anyway
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,382
    So have you tried that yet?

    Edit: just to clarify. What I mean is - Your code does not exactly do that, unless you are certain that the player is directly in front of the camera at all times.
     
    Last edited: Jan 27, 2022
  5. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    yes, the player is always in the same position compared to the camera, It uses a virtual camera and it's always framing the player at the same place, basically in the centre, no rotation, fixed angle
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    Raycast fires a ray, and returns the first (i.e. closest) collider it hits.
    RaycastAll fires a ray, but the ray does not stop at the first collider, it continues piercing through everything until it reaches maximum distanc. Then it returns all objects it hit.
     
  7. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    yeah, i got that ^_^ like i said, I mis read how it was written. Not something i don't have a habbit for.
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,996
    BTW, I recommend to use BoxCastAll instead of RayCast anyway, since the player has volume, it is easier to approximate its size so you can hide most things get in the way. A RayCast only tests a line, a BoxCast tests a volume.
     
    kdgalla likes this.
  9. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    mm.. reading up on it.. might be worth a try.. though while i'm trying it.. something isn't working out right, or atleast no different..

    I would assume that, as i'm looking for things that are between the camer and the player, do i box around the player or the camera.. i would have thought camera.. but using the forward of the camera to get the direction it's facing for the direction doesn't seam to find my tree (which has a capsule collider and set as trigger just so it should find it). I even tried giving the camera rotation but.. finds stuff more on ground level but not where the tree is..
     
  10. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    damn it.. been a bit busy of late but took a look at it while debuging.. it wasn't creating the tree object from the prefab but just the mesh.. so it didn't add the collider nor the tag that should have been there.. stupid mistake ¬_¬
     
  11. Innovine

    Innovine

    Joined:
    Aug 6, 2017
    Posts:
    522
    This isn't what you are doing. You are looking for things directly in front of the camera.

    Have you checked the layers setup and which layers interact with each other?
     
  12. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    nope. Like I said, turned out there was a minor mistake where it was adding a Mesh to the level and not the prefab, so the collider and tag added to the prefab wasn't there so it didn't work. changing this so it creates the prefab as i should, it works fine now