Question by
allaloud1 · Sep 28, 2019 at 03:25 PM ·
2dscripting problemandroid build2d-physicstouch controls
no error in program but code isn't working,no problem at my script but it isn't working. PLS HELP
im using unity 2019.2.6f1 and visual studio 2019. the code i have should make me able to drag and drop an object, the script is running and im not getting any error but it is not doing its job to let me drag and drop an object. i don't know if there's something i forgot or wrong in the script thats why it isnt working. please check my script and tell me whats wrong. TIA
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DragAndDrop : MonoBehaviour {
bool moveAllowed;
Collider2D col;
// Start is called before the first frame update
void Start()
{
col = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
if (touch.phase == TouchPhase.Began)
{
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
if (col == touchedCollider)
{
moveAllowed = true;
}
}
if (touch.phase == TouchPhase.Moved)
{
if (moveAllowed)
{
transform.position = new Vector2(touchPosition.x, touchPosition.y);
}
}
if (touch.phase == TouchPhase.Ended)
{
moveAllowed = false;
}
}
}
}
Comment