Answer the question
In order to leave comments, you need to log in
How is the mechanism for buying towers implemented in Bloonds td6?
How did they make it so that when you click on the desired tower, it appears in your hand and until you release the mouse, it will follow it?
It seemed to me that when you click on the button / icon of the tower, an object is immediately created and while the mouse is pressed, it constantly reads the position of the mouse position and when you release it, it checks whether there is something in this place that your collider wants to touch and if there is nothing, then it is put finally. but! I wildly doubt, and even if so, how do they do it?
I found a method that seems to be necessary, but it does not work, it only catches clicks
public void OnMouseDrag()
{
Debug.Log("da");
}
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Answer the question
In order to leave comments, you need to log in
After a week of torment, I did it and quite by accident. Might be useful to someone
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class BuyTowers : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public GameObject PistolTowerGive;
public GameObject PistolTowerSet;
public bool UseTower;
public Vector2 mousePos;
private GameObject TowerPistol;
public void Start()
{
}
public void OnPointerDown(PointerEventData pointerEventData)
{
TowerPistol = Instantiate(PistolTowerGive, transform.position, Quaternion.identity);
UseTower = true;
}
public void OnPointerUp(PointerEventData pointerEventData)
{
UseTower = false;
}
public void FixedUpdate()
{
if (UseTower == true)
{
TowerPistol.transform.position = mousePos;
}
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question