- Home /
The question is answered, right answer was accepted
Raycast only works in cartain areas of scene
I am building an RTS style city building game and am havig an issue which I believe is connected to raycasts and the camera. I am using raycasts to determine where on the ground buildings are to be placed, however I cannot place buildings in certain areas towards the top and bottom of my map and I am unsure why. I am using an orthographic camera and if I change the position, or clipping planes of the camers the areas where I am unable to place buildings also changes.
Any help would be appreciated, thanks in advance.
Answer by Geva93 · Jul 20, 2015 at 10:42 PM
I think its because you dont use layers, check if the camera ray cast is on the all layers.
I am using this code to only check layer 10, which is the "terrain" layer. I may be using it incorrectly, I am new to raycasts.
RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 400, 1 << 10)) { p = hit.point; }
Define the layer in advance as a type Layer$$anonymous$$ask
Layer$$anonymous$$ask myLm = 1 << 10;
I do believe if you dont do this 1 << 10 could register as an int which is not what it wants.
I believe that I am telling the ray cast to only apply to the "terrain" layer, which is layer 10 using this code
RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 400, 1 << 10)) { p = hit.point; }
@$$anonymous$$kin Please post non-answers as comments and format code with 101010 button, thanks,
thanks I have made this change now, and can now place buildings correctly anywhere on half of my map, however if I pan to the right and attempt to place objects on the other half then the buildings stop following the mouse until I bring the cursor back onto the first half of the scene. All terrain objects are placed in the correct layer, could this be an issue with how I am moving my camera?
Follow this Question
Related Questions
Smooth camera on moving platform 1 Answer
Unity Android Movement Problem 0 Answers
How to move a camera only using the arrow keys? 7 Answers
Detect Mouse in right side or left side For Player? 2 Answers
Camera to aim at raycast 1 Answer