- Home /
GetMouseButtonDown strange behaviour
Hello everyone! I am trying to detect when a player clicks on a piece of a block and then delete it. The code works perfectly fine with the exception of it requiring me actually clicking twice on the block, rather than once. Any ideas?
Code --
PlayerIO script (attached to camera on FPS controller):
void Update() { if(!Input.GetMouseButtonDown(0)) return; Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.5f)); RaycastHit hit; if(Physics.Raycast(ray, out hit, interactionRange)) { Chunk chunk = hit.transform.GetComponent (); if(chunk == null) { return; } chunk.SetBrick(0, (int)hit.point.x, (int)hit.point.y, (int)hit.point.z); } }
chunk.SetBrick function (attached to the chunk itself):
public bool SetBrick(byte brick, int x, int y, int z) { if((x < 0) || (y < 0) || (z < 0) || (x >= width) || (y >= height) || (z >= width)) return false; if(map[x, y, z] == brick) return false; map[x, y, z] = brick; StartCoroutine(CreateVisualMesh()); return true; }
Any help is greatly appreciated!
PS: I have no idea how to make the code thing, sorry. I'm a newb :c
Anything with four spaces in front of it is treated as code. Alternatively, you could click the button above the input box with the 1s and 0s, and paste your code in there. That won't help with your lack of line breaks, though.
I copied and pasted the code into an editor and took a look. There is nothing about deleting/destroying/removing in your code. Your code does not have sufficient information for anyone to debug.
void Update()
{
if(!Input.Get$$anonymous$$ouseButtonDown(0))
return;
Ray ray = camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.5f));
RaycastHit hit;
if(Physics.Raycast(ray, out hit, interactionRange))
{
Chunk chunk = hit.transform.GetComponent();
if(chunk == null)
{
return;
}
chunk.SetBrick(0, (int)hit.point.x, (int)hit.point.y, (int)hit.point.z);
}
}
public bool SetBrick(byte brick, int x, int y, int z)
{
if((x < 0) || (y < 0) || (z < 0) || (x >= width) || (y >= height) || (z >= width))
return false;
if(map[x, y, z] == brick)
return false;
map[x, y, z] = brick;
StartCoroutine(CreateVisual$$anonymous$$esh());
return true;
}
Thank you all for your quick replies. I have managed to fix the issue. $$anonymous$$y problem was that I had two chunks instantiated at the same location, so basically I had to delete two blocks ins$$anonymous$$d of just one. I am sorry for my lack of experience.