Answer the question
In order to leave comments, you need to log in
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)
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;
}
}
...........
private Dictionary<(int,int),GameObject> Cells = new Dictionary<(int,int),GameObject>();
...........
Cells[(x,y)] = Instantiate(Gift, new Vector2(x, y), Quaternion.identity);
...........
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question