1

I started to learn about Unity, and I have a problem with the Raycasting. So I have this code

   if(Physics.Raycast(cam.position,cam.forward,out hit,100f,layer)){
    Debug.Log(hit.point);
    if(hit.transform!=this.transform){
     showPreview(hit);   
     print(hit.transform.name);
    }
   }

and it works fine with objects and anything else, except the terrain. If the terrain is Flat, the raycast doesn't detect it, but if I make some mountains it works fine. Flat terrain, zero log / Tiny hill, it detects I tried to change the distance, but it didn't work.

Nagy Márk
  • 11
  • 2
  • 1
    Are you sure you are hitting the terrain within the 100 units? What happens if you rotate the camera more downwards and move closer to the terrain? – derHugo Jul 04 '20 at 14:35
  • I would also triple check layers. moving scenes between project can mess up layer references – zambari Jul 04 '20 at 23:44

1 Answers1

0

I found the problem. In the script i used the fps character as camera, and it didn't detect the vertical rotation :D Thanks for the help!

Nagy Márk
  • 11
  • 2