Calculation Error, please send help.
Hi! So, I'm working on a game in Unity and I am running in to a few issues. I have narrowed this down to error on my end, but I'm not sure what exactly it is. What I'm working on is 2D, block - based terrain, and I am currently doing terrain editing. The problem is, I can't seem to edit blocks if either the X or Y coordinate is below zero. Here's my code:
Vector2 pos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 cPos = new Vector2(((int)pos.x / World.chunkSize) * World.chunkSize, ((int)pos.y / World.chunkSize) * World.chunkSize);
Debug.Log(cPos);
pos.x += 0.5f;
pos.y += 0.5f;
Chunk c = Chunk.GetChunkAt((int)cPos.x, (int)cPos.y);
if(c != null)
{
c.RemoveBlock((int)(pos.x - cPos.x), (int)(pos.y - cPos.y));
}
So basically, I round the mouse position to a Chunk position in world coordinates. Then, I get that chunk and remove the specified block. I have the 'remove block' method to just return if either X or Y is less than zero, X or Y is greater than the chunk size. Otherwise, remove the specified block.
Here's my GetChunk code: public static Chunk GetChunkAt(int x, int y) { foreach(Chunk c in allChunks) { if(c.transform.position.x == x && c.transform.position.y == y) return c; }
return null;
}
And my RemoveBlock code:
public void RemoveBlock(int x, int y) { if(x < 0 || y < 0 || x >= World.chunkSize || y >= World.chunkSize) return;
map[x,y] = 0;
for(int i = 0; i < transform.childCount; i++)
{
if(transform.GetChild(i).transform.position == new Vector3(x,y,0) + this.transform.position && transform.GetChild(i).GetComponent<SpriteRenderer>().sortingOrder == 1){
Destroy(transform.GetChild(i).gameObject);
return;
}
}
}
"The problem is, I can't seem to edit blocks if either the X or Y coordinate is below zero."
"I have the 'remove block' method to just return if either X or Y is less than zero"
Doesn't the second statement explain the first?
Not really, because I convert the position to chunk coordinates (which may not be working properly), then I input it to the function.
Hmm, think we will need to see your RemoveBlock function and chunk class. For example, I don't see why you pass parameters to RemoveBlock.. doesn't the chunk already know where it is? And why do you pass it an offset(subtraction), rather than the actual chunk coordinates used by GetChunkAt?
Well, the 'GetChunk(x,y)' function returns a Chunk at the specified world position. Then, 'RemoveBlock(x,y)' removes the block at the specified LOCAL position of the block. I'll add my code to the question.
hmm, so each chunk position defines the corner of a chunk (blockX,blockY both > 0) the number of possible blocks in a chunk is World.chunkSize^2 (squared), right?
AH! Can I ask you to confirm, with some debug logs, that this line is actually doing what you expect: Vector2 pos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
I think you are passing an incorrect parameter. The Vector2 mouse position, is converted into a Vector3, with a Z-component of Zero. https://docs.unity3d.com/ScriptReference/Vector3-ctor.html So, when the ScreenToWorld function computes the world point, is uses a distance from the camera of Zero: which results in the camera itself's world position. https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
I think you will need to compute the distance, along the camera view angle (use the vector camera.transform.forward), to the plane you want (plane: z=0). (lots of stuff out there for that: here's my "I'm feeling lucky" result- http://mathinsight.org/distance_point_plane)
Your answer
![](https://koobas.hobune.stream/wayback/20220612104247im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Compiler Error, Cannot Go Into Playmode 2 Answers
HELP!! unexpected token: If 1 Answer
Error CS8025: Parsing Error 3 Answers