Reputation: 5007
I have the following script attached to my game object:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchDragDrop : MonoBehaviour
{
// Components
private Rigidbody2D myRigidbody2D;
private CanvasGroup canvasGroup;
private Collider2D myCollider2D;
// Drop Zones
[SerializeField] public List<GameObject> dropZones = new List<GameObject>();
// Indicators
private bool isMoving = false;
private bool debuglog = true;
public bool isDropped = false;
// Numbers
private Vector2 touchPosition;
private float deltaX, deltaY;
private Vector2 oldVelocity;
// Start is called before the first frame update
void Start()
{
myRigidbody2D = GetComponent<Rigidbody2D>();
myCollider2D = GetComponent<Collider2D>();
canvasGroup = GetComponent<CanvasGroup>();
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
// touchPosition = touch.position;
touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
switch (touch.phase)
{
case TouchPhase.Began: beginTouchMove(); break;
case TouchPhase.Moved: moveTouch(); break;
case TouchPhase.Stationary: stationaryTouch(); break;
case TouchPhase.Ended: endTouchMove(); break;
}
}
}
private void beginTouchMove()
{
if (myCollider2D == Physics2D.OverlapPoint(touchPosition))
{
if(debuglog) Debug.Log("Begin Touch @ x: " + touchPosition.x.ToString() + " y: " + touchPosition.y.ToString());
//if(debuglog) Debug.Log("RigidBody: " + myRigidbody2D.position.ToString());
deltaX = touchPosition.x - transform.position.x;
deltaY = touchPosition.y - transform.position.y;
isMoving = true;
myRigidbody2D.bodyType = RigidbodyType2D.Kinematic;
oldVelocity = myRigidbody2D.velocity;
myRigidbody2D.velocity = new Vector2(0f, 0f);
}
}
private void moveTouch()
{
if (isMoving)
{
Vector2 newPosition = new Vector2(touchPosition.x - deltaX, touchPosition.y - deltaY);
//gameObject.transform.position = newPosition;
myRigidbody2D.MovePosition(newPosition);
//if(debuglog) Debug.Log("Touch Position: x: " + touchPosition.x.ToString() + " y: " + touchPosition.y.ToString());
//if(debuglog) Debug.Log("RigidBody: " + myRigidbody2D.position.ToString());
}
}
private void endTouchMove()
{
if (debuglog) Debug.Log("On End Touch");
canvasGroup.blocksRaycasts = true;
// Check drop zones for overlap
foreach(GameObject dropZone in dropZones)
{
if (debuglog) Debug.Log("Checking " + dropZone.name);
if(dropZone.GetComponent<Collider2D>() == Physics2D.OverlapPoint(myCollider2D.transform.position))
{
isDropped = true;
if (debuglog) Debug.Log("TOUCH: Found Collider");
dropZone.GetComponent<DropZone>().EndGameObjectLife(gameObject);
}
}
if(!isDropped) {
myRigidbody2D.bodyType = RigidbodyType2D.Dynamic;
myRigidbody2D.velocity = oldVelocity;
}
}
private void stationaryTouch()
{
//Debug.Log("Stationary Touch");
}
}
My problem is I dont know how to check that the Collider in gameObject has hit one of the colliders in the dropZones List. I dont get any errors, but I don't get the message "Touch: Found Collider" either. So the game doesnt acknoledge that the object has found its target.
What is the best way to go about this?
I tried doing
if(dropZone.GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPosition))
Which works to a point, but it does mean that the user can just touch the target without dragging the object and it'll recognize a drop. With the MouseEvents I have Ondrop, is there an equivilent for Touch?
Upvotes: 0
Views: 368
Reputation: 2842
To check if a collider hit other collider you can use Collider2D messages like OnCollisionEnter2D, OnCollisionExit2D, OnCollisionStay2D. There are similar messages if your collider is set to trigger like OnTriggerEnter2D.
Once you detect the collision check if the collider is in the dropZones list.
Upvotes: 1