Question by
UltraProgrammer · Feb 19, 2018 at 12:59 AM ·
raycastraycastingminecraftblocksplacing
Placing blocks on grid with ray-casting
I'm working on a game, similar to Minecraft, I can't seem to figure out how to pull off placing blocks. My first idea was to fire a raycast, find the coordinates at which it hit, then round each coordinate to the nearest whole number. While this did ensure that the blocks snapped to the grid, they now sometimes spawn inside other blocks. Does anyone know an easier way to do this? This is the code placed inside the blocks that causes them to snap to the grid (C#):
private float PositionX;
private float PositionZ;
private float PositionY;
private GameObject Player;
// Use this for initialization
void Start () {
Player = GameObject.Find("FPSController");
PositionZ = Mathf.Round(gameObject.transform.position.z);
PositionY = Mathf.Round(gameObject.transform.position.y);
PositionX = Mathf.Round(gameObject.transform.position.x);
gameObject.transform.position = new Vector3 (PositionX, PositionY, PositionZ);
}
Thanks in advance
Comment
Your answer
Follow this Question
Related Questions
I'm Trying to get a 3D Model to follow a Raycast 0 Answers
I can't get RayCast to report the collider I want. 1 Answer
Blink, Dash, Raycasting 0 Answers
Is there any way how to do raycasts like this? 0 Answers
Raycasting not detecting collider 0 Answers