- Home /
Snapping Issue
Ok so I have this script and have a snapping issue that makes no sense. I create a chunk with a for statement and then I create a few structures with for statements also. When I place a block it usually work but sometimes it decides to place the block inside another(same location). How can I eliminate this issue? I think its a rounding issue! Also while I have attention is there a way to not have the mouse and center this action where you create the gui crosshair? How can I make the gui crosshair? Just use the games gui function?
void Update()
{
Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
Vector3 G = new Vector3(Mathf.Round(hit.point.x), Mathf.Ceil(hit.point.y), Mathf.Round(hit.point.z));
if(Physics.Raycast(ray, out hit))
{
if(Input.GetMouseButtonDown(0))
{
Destroy(hit.collider.gameObject);
}
if(Input.GetMouseButtonDown(1))
{
Instantiate(prefab, G, Quaternion.identity);
}
}
}
Answer by Carbongrip · Mar 30, 2014 at 04:09 AM
Well I finally figured it out after taking a break from the project... I was so close with original code. This is what worked out for me...
void Update()
{
Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
if(Physics.Raycast(ray, out hit))
{
if(Input.GetMouseButtonDown(0))
{
Destroy(hit.collider.gameObject);
}
if(Input.GetMouseButtonDown(1))
{
if(prefab != null)
Instantiate(prefab, hit.transform.position + hit.normal, Quaternion.identity);
else
return;
}
}
Answer by RudyTheDev · Feb 22, 2014 at 12:48 PM
The best I understand is that you have a voxel-based world type game. You pick a position a short bit in front of the camera (round it off) and place a block there. But you aren't doing anything in the code above to check if there is already a block there. So nothing is stopping the game from creating the block at the same place as other blocks.
The fix is "simple" -- you don't create a block if there is one already there. (Usually, voxel-based games place blocks next to the face of the block you are looking at, but it seems you simply place it in front of the camera.) So, in your case, you probably want Physics.OverlapSphere()
or similar to detect if there is something already there at G
. In the simplest case:
if (Physics.OverlapSphere(G, 0.1f).Length == 0) // 0 colliders are within 0.1f around location G
// Your block placing code
Well that prevents objects from being placed where they shouldn't but then they hardly ever place. I do however don't have a voxel world, I found a better solution to the fps problem. This is because I tried voxels and didn't understand it enough. How can I make this work with simple cubes?
I don't quite understand your reply. Anyway, I didn't say you have voxel-based world, I said voxel-based world type game. It is usually done with voxels, but you can do it with whatever fits, like GameObject cubes, if that works. $$anonymous$$y reply didn't assume voxels, in fact, it wouldn't even work with a true voxel mesh.
So what exactly does not work? Objects not placed where they shouldn't sounds like it "works". Perhaps you haven't explained what it is that you want to begin with?
I just have a issue placing objects now. Like now it won't place it in a bad spot but you can't get it to place at that point ever.
Also noticed it always places the block on other blocks spawned with this function just picky with the generated objects from for loops.
Edit: Just noticed its happening when I try placing on sides of blocks. So when I try to place on sides sometimes it does nothing.
re: the crosshair
Create a small graphic (24x24ish) and place it at screen.width/2, screen.height/2; to turn off normal cursor there's a standard function for that- screen.cursor = off/false or something like that do a search
@Carbongrip: "it won't place it in a bad spot but you can't get it to place at that point ever" -- make sure there are no collisions there. Iterate through the Physics.OverlapSphere(G, 0.1f)
array and see what it actually collides with at that point (and what that point really is).
"it always places the block on other blocks spawned with this function" -- that's may be because you haven't attached a collider to them, so the no-collision check passes.
"try placing on sides of blocks" -- you need to post your "place on sides of blocks" code. The code in the question doesn't include any logic for this.
P.S. It would also help you with debugging if you made a dummy block that constantly repositions itself to where the G
is.