Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. public class PlayerNew : MonoBehaviour
  5. {
  6. private Camera _camera;
  7. private Vector3 _direction;
  8. private Vector3 _offset;
  9. private Rigidbody2D _rb2d;
  10. private Vector3 _target;
  11.  
  12. private void Awake()
  13. {
  14. _camera = Camera.main;
  15. _rb2d = GetComponent<Rigidbody2D>();
  16. }
  17.  
  18. void Update()
  19. {
  20. GetInput();
  21.  
  22. _target = _direction + _offset;
  23. }
  24.  
  25. private void FixedUpdate()
  26. {
  27. Move();
  28. }
  29.  
  30. private void GetInput()
  31. {
  32. if (Input.GetMouseButton(0))
  33. {
  34. _direction = _camera.ScreenToWorldPoint(Input.mousePosition);
  35.  
  36. _direction.z = 0;
  37. }
  38.  
  39. if (Input.GetMouseButtonDown(0))
  40. {
  41. _offset = transform.position - _direction;
  42. }
  43. }
  44.  
  45. private void Move()
  46. {
  47. _rb2d.MovePosition(_target);
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement