Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Vector3 touchPosition;
- private Rigidbody2D rb;
- private Vector3 direction;
- private float moveSpeed = 10f;
- static GameObject go;
- private void Start()
- {
- rb = GetComponent<Rigidbody2D>();
- }
- private void Update()
- {
- if (Input.touchCount > 0 && gameObject == go)
- {
- Touch touch = Input.GetTouch(0);
- touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
- touchPosition.z = 0;
- direction = (touchPosition - transform.position);
- rb.velocity = new Vector2(direction.x, direction.y) * moveSpeed;
- GetComponent<BoxCollider2D>().enabled = false;
- if (touch.phase == TouchPhase.Ended)
- rb.velocity = Vector3.zero;
- }
- else
- {
- GetComponent<BoxCollider2D>().enabled = true;
- }
- }
- private void OnMouseDown()
- {
- go = gameObject;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment