Home > Software design >  Block based coding Unity 2d Games
Block based coding Unity 2d Games

Time:09-29

does anyone know how to create a trigger for a game object to move? To be more precise, I wanna make a 2d game on Unity where the user can drag and drop arrow (left,right, up and down) on the field and the game object can move accordingly to the arrow in the field?? And they can also put the loop inside the field to make a repetitive moves. The example is in the picture. The UI of my game I really dont have much idea cuz i have 0 experience in unity and this is for my final year project. For reference, i wanna make games like Kidlocoding or codemonkey. Thanks a lot!

CodePudding user response:

for each of the arrows you could add a vector3 for that value to the bee's position.

arrows[]  = {Vector3.right,Vector3.left,Vector3.down,Vector3.up};


   float tileWidth; // the width/height of the tile
  
   
   foreach(Vector3 arrow in arrows)
    {
       transform.position  = arrow * tileWidth;

    }

CodePudding user response:

Add a public variable for each of the buttons.
Create a function for each of your buttons (LeftClick, RightClick, etc..).
Then bind each buttons onClick event to the corresponding function.

Below is an example using the left button.

// Assign these in the inspector
//
public Button leftButton;
public RectTransform bee; 
public float tileWidth;


private void Start()
{
    leftButton.onClick.AddListener(LeftButtonClick);
}

private void LeftButtonClick()
{
    Debug.Log($"Left Button Clicked!");

    // Move bee to the left
    //
    bee.anchoredPosition  = Vector2.left * tileWidth;
}
  • Related