How can I get the X,Z value at a height of Y along a raycast?

So I’m trying to get the X and Z position at which a raycast intersects at a certain Y level.

I’m using this in a base building system. The player is shown a preview ghost for the building section they want to place but I need the position from the raycast to position the preview ghost on the correct coordinates. I’m using some Y values as levels the player can place building sections on, so I want to use the Y values and the information from the raycast to find the point. I just don’t know the math or functions to find this.

1 Answer

1

You’re gonna need to shoot the raycast from the camera, so documentation on all that is required for this is:

Also screen to world is very important : Unity - Scripting API: Camera.ScreenToWorldPoint

and you’ll probably also need to get familiar with collisions : Unity - Scripting API: Collision.gameObject

Also might need to understand the Quaternions : Unity - Scripting API: Quaternion

but mostly will need to know about positions : Unity - Scripting API: Transform.position

and from the sounds of it, you’ll need to know about colors : Unity - Scripting API: Color

pretty sure there is something else, but it escapes me at the moment… But you should be good to go :slight_smile:

Ohh yeah, instantiations : Unity - Scripting API: Object.Instantiate

I have everything like that working, I'm not completely new I just need the math for the raycast is all. Currently I have it working with detecting a collision with a certain layermask and checking if the object is a building area collider. But I want to remove having the colliders so I can have multiple building levels without having to check for which collider its hitting and so on. It all functions fine I just need help with the math but thanks anyway.