All object move together when try to drag one object only
Hello, So I try to make spelling game by doing drag and drop the alphabet at the correct place. So far the code is worked for me since I learned through tutorial video but the problem is, when I try to drag one alphabet, all other alphabets move along too and I can move it even though I didn't touch that object yet (means I can drag it at any spot)... Here my code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DragFoxX : MonoBehaviour {
[SerializeField]
private Transform FoxX;
private Vector2 initialPosition;
private float deltaX, deltaY;
public static bool locked;
void Start()
{
initialPosition = transform.position;
}
private void Update()
{
if (Input.touchCount > 0 && !locked)
{
UnityEngine.Touch touch = Input.touches[0];
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began:
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos))
{
deltaX = touchPos.x - transform.position.x;
deltaY = touchPos.y - transform.position.y;
}
break;
case TouchPhase.Moved:
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos))
transform.position = new Vector2(touchPos.x - deltaX, touchPos.y - deltaY);
break;
case TouchPhase.Ended:
if (Mathf.Abs(transform.position.x - FoxX.position.x) <= 0.5f &&
Mathf.Abs(transform.position.y - FoxX.position.y) <= 0.5f)
{
transform.position = new Vector2(FoxX.position.x, FoxX.position.y);
locked = true;
}
else
{
transform.position = new Vector2(initialPosition.x, initialPosition.y);
}
break;
}
}
}
} .
This is my first time using Unity and learning about developing the game, so I'm not sure which part I do wrong. Thank you.
Comment