Y
Y
yraiv2022-01-29 19:39:03
Unity
yraiv, 2022-01-29 19:39:03

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");

    }


This is the first. And the second - how to constantly update the position? I found this option -
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

but I can’t push it into update or fixedupdate. He will eat resources all the time, and I need to call him only at a certain moment, namely at the time of purchase. I thought to check for a bool variable in the update, saying whether the purchase is active now (whether the person pinched his finger), but it also eats resources ... I don’t understand ... Are there those who have encountered this or similar? how to proceed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yraiv, 2022-01-30
@yraiv

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 question

Ask a Question

731 491 924 answers to any question