Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Player : MonoBehaviour
- {
- [SerializeField] private float speed;
- private Camera camera;
- private Vector2 touchPosition;
- private Vector2 offset;
- private Vector2 direction;
- private Rigidbody2D rb2d;
- private void Awake()
- {
- rb2d = GetComponent<Rigidbody2D>();
- camera = Camera.main;
- Input.multiTouchEnabled = false;
- direction = transform.position;
- }
- private void Update()
- {
- GetInput();
- }
- private void FixedUpdate()
- {
- rb2d.MovePosition(direction);
- }
- private void GetInput()
- {
- if (!Input.GetMouseButton(0)) return;
- touchPosition = camera.ScreenToWorldPoint(Input.mousePosition);
- if (Input.GetMouseButtonDown(0))
- {
- offset = (Vector2)transform.position - touchPosition;
- }
- if (Input.GetMouseButton(0))
- {
- direction = touchPosition * speed + offset;
- }
- if (Input.GetMouseButtonUp(0))
- {
- direction = transform.position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement