Home > Back-end >  How do i fix the placement tool in my level editor?
How do i fix the placement tool in my level editor?

Time:05-21

So i'm currently making a game, and i've recently added a level editor, but the placing tool does not work how i wanted it to.

https://youtu.be/MuUvnVTL6eg

If you've watched this video, you've probably realized that the block placing works pretty much how placing rectangles in ms pain with alt does, and i want it to work like placing rectangles in ms pain without alt xd.

I'm using this code to place the block:

if (Input.GetKeyDown(KeyCode.Mouse0)){
    startDrawPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    tmpObj = spawnObject(blocks[selected].gameObject, startDrawPos);
    drawing = true;
}

if (Input.GetKey(KeyCode.Mouse0)){
    Vector2 mPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector2 tmpScale = new Vector2(startDrawPos.x - mPos.x, startDrawPos.y - mPos.y);
    tmpObj.transform.localScale = tmpScale;
}

if (Input.GetKeyUp(KeyCode.Mouse0))
{
    drawing = false;
    var scale = tmpObj.transform.localScale;
    //Code below destroys the object if it's too small to avoid accidental placements
    if (scale.x <= 0.1 && scale.x > -0.1 || scale.y <= 0.1 && scale.y > -0.1)
    {
        Destroy(tmpObj);
    }
}

(All of this code is in the Update() function) (spawnObject function just instantiates the object prefab)

There is a bit more code but it has nothing to do with the position of the block, it just detect which block is selected and decides if it can be resized or not.

CodePudding user response:

I solved this problem. But because your complete script is not in question, I rebuilt the code with IEnumerator, Here, by pressing the left mouse button, IEnumerator is activated and all commands are grouped in one method to make the code more efficient.

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0)) StartCoroutine(DrawRect());
}

How does the Desktop Rect formula work?

By running IEnumerator, the code first records the starting point of the mouse. It also makes a simple cube because I do not have access to your objects. Now until the mouse is pressed. Resize Rect to the difference between current and recorded points. The only thing is that to avoid ALT control, you have to place it between the current and initial points. The reason for adding the camera forward is to be seen in the camera.


cubeObject.transform.position = (startDrawPos   currentDrawPos) / 2;

The final structure of the DrawRect is as follows:

public IEnumerator DrawRect()
{
    drawing = true;
    var scale = Vector2.zero;
    
    var startDrawPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    
    var cubeObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    
    while (Input.GetKey(KeyCode.Mouse0))
    {
        var currentDrawPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        cubeObject.transform.position = (startDrawPos   currentDrawPos) / 2   Camera.main.transform.forward * 10;
        
        scale  = new Vector2(startDrawPos.x - currentDrawPos.x, startDrawPos.y - currentDrawPos.y);
        
        cubeObject.transform.localScale = scale;
        
        yield return new WaitForEndOfFrame();
    }
    
    if (scale.x <= 0.1 && scale.x > -0.1 || scale.y <= 0.1 && scale.y > -0.1) Destroy(cubeObject);
    drawing = false;
}
  • Related