- Home /
Question by
elzasepasts · May 23, 2020 at 12:33 PM ·
mousepositiontouch controlstouchscreenmouseclickmouse input
Touch input to Mouse input || Simple drag drop
I'm having a hard time with Unity and wanted to follow a tutorial to make a game, but it's touch imput, i want it to work on computer. Been trying to change it for 2 days, but no success. Please help!
Here's the tutorial for context. https://www.youtube.com/watch?v=sf9_ingF57s&t=497s
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NoCrash : 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