Z
Z
Zimaell2020-10-13 15:01:29
Unity
Zimaell, 2020-10-13 15:01:29

Why do all objects move at once?

I have a prefab and this script is attached to it (responsible for moving it with the mouse)

spoiler
class Move : MonoBehaviour{
    public GameObject gift;
    private bool mooving;
    private float startPosX;
    private float startPosY;

    void Update(){
        if(mooving){
            Vector3 mousePos;
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);
            this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.transform.localPosition.z);
            }
        
        if (Input.GetMouseButtonDown(0)) OnMouseDown();
        if (Input.GetMouseButtonUp(0)) OnMouseUp();
        }

    private void OnMouseDown(){
            Vector3 mousePos;
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);
            startPosX = mousePos.x - this.transform.localPosition.x;
            startPosY = mousePos.y - this.transform.localPosition.y;
            mooving = true;
        }
    
    private void OnMouseUp(){
        mooving = false;
        }
    }

there is a script that displays them in different coordinates
...........
private Dictionary<(int,int),GameObject> Cells = new Dictionary<(int,int),GameObject>();
...........
Cells[(x,y)] = Instantiate(Gift, new Vector2(x, y), Quaternion.identity);
...........

The problem is that when you move one of the objects, they move all at once.
How to move them one by one, that is, move the one with the mouse?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-10-13
@tentrun

Add a trigger to each object and check already then, so you will get all the objects that are gift

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question