Home > OS >  Moving the button in Unity
Moving the button in Unity

Time:04-07

I have buttons with letters and I need to make a word out of them, please tell me how can I move this button to the field after clicking on the button with a letter? Or is it better to do something differently?

i.e. by pressing the button with the letter S, the button disappears from that place and moves to the field

CodePudding user response:

I'm kind of new to Unity but this is what I think you would need to do.

If you want the letter S to move when you press the SKey, you would probably want something like this:

void Update()
{
    if (Input.GetKeyDown(KeyCode.S))
    {
        MoveS();
    }
}

void MoveS()
{
    objectS.transform.localPosition = new Vector3(newXPos, newYPos, newZPos);
}

where the new Vector 3 is the position where you want the letter to move. You would probably need to repeat this for each letter you want to use the keyboard for.


If you want the letter S to move when you hover over it and click on it, you would probably want something like this:

public void MoveS()
{
    objectS.transform.localPosition = new Vector3(newXPos, newYPos, newZPos);
}

and then assign it to the 'On Click()' section on Unity for the button. Here's what that section looks like

CodePudding user response:

If you want it to move, you could do

public GameObject letter; //define the letter
public GameObject emptyField; // define empty field
void Update()
{
    if (Input.GetKeyDown(KeyCode.S))
    {
        letter.transform.position = emptyField.transform.position; //letter moves to field
    }
}

You could make it whatever key you want by replacing S with whatever

  • Related