- Home /
How to transition a click-and-drag from a UI button to a GameObject
For a tower defense game, right now the way I have it working is you click one of these tower buttons:
and the tower appears in the center of my map. You then click and drag the tower to wherever you want it to go. How would I go about making it so you can click and drag the GameObject directly from the button? Can a UI canvas button object pass 'drag' events to a GameObject that it can instantiate? Basically I'm trying to get it to function similar to how they have it on Bloons TD 6.
Any thoughts or suggestions would be greatly appreciated!
Here is my code on the tower GameObject for managing the click and drag:
public void OnBeginDrag(PointerEventData eventData) { if (!beingPlaced) return; }
public void OnDrag(PointerEventData eventData)
{
if (!beingPlaced) return;
if (beingPlaced == true)
{
var pos = Camera.main.ScreenToWorldPoint(new Vector3(eventData.position.x, eventData.position.y, 0f));
this.transform.position = new Vector3(pos.x, pos.y, 0);
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (!beingPlaced) return;
if (cannotPlaceTower == false || collisionCount == 0)
{
gameController.placingTower = false;
uiController.cancelTowerButton.SetActive(false);
gameController.gameMusic.PlayOneShot(gameController.towerPlace, 1f);
beingPlaced = false; //place tower
towerRange.hideRangeCircle();
gameController.addEnergy(-towerCost);
}
}
public void OnPointerDown(PointerEventData eventData)
{
if (beingPlaced == false)
{
if (gameController.placingTower)
gameController.stopTowerBeingPlaced();
calculateDPS();
uiController.towerPanel.GetComponent<TowerPanelController>().showTowerPanel(this);
}
}
Your answer
Follow this Question
Related Questions
How to use gameObject as a drag-able object. 1 Answer
OnBeginDrag, OnEndDrag not being called for unknown reasons 1 Answer
How to find in what Scripts or components an Object is connected 0 Answers
Drag and drop files from the assets folder to another folder not working 0 Answers
How I can drag objects(songs) into the game(at runtime) and save it? 0 Answers