Guest User

Untitled

a guest
May 8th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. private Vector3 touchPosition;
  2. private Rigidbody2D rb;
  3. private Vector3 direction;
  4. private float moveSpeed = 10f;
  5.  
  6. static GameObject go;
  7.  
  8. private void Start()
  9. {
  10. rb = GetComponent<Rigidbody2D>();
  11. }
  12.  
  13. private void Update()
  14. {
  15. if (Input.touchCount > 0 && gameObject == go)
  16. {
  17. Touch touch = Input.GetTouch(0);
  18. touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
  19. touchPosition.z = 0;
  20. direction = (touchPosition - transform.position);
  21. rb.velocity = new Vector2(direction.x, direction.y) * moveSpeed;
  22. GetComponent<BoxCollider2D>().enabled = false;
  23.  
  24. if (touch.phase == TouchPhase.Ended)
  25. rb.velocity = Vector3.zero;
  26. }
  27. else
  28. {
  29. GetComponent<BoxCollider2D>().enabled = true;
  30. }
  31. }
  32.  
  33. private void OnMouseDown()
  34. {
  35. go = gameObject;
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment