Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- public class PlayerNew : MonoBehaviour
- {
- private Camera _camera;
- private Vector3 _direction;
- private Vector3 _offset;
- private Rigidbody2D _rb2d;
- private Vector3 _target;
- private void Awake()
- {
- _camera = Camera.main;
- _rb2d = GetComponent<Rigidbody2D>();
- }
- void Update()
- {
- GetInput();
- _target = _direction + _offset;
- }
- private void FixedUpdate()
- {
- Move();
- }
- private void GetInput()
- {
- if (Input.GetMouseButton(0))
- {
- _direction = _camera.ScreenToWorldPoint(Input.mousePosition);
- _direction.z = 0;
- }
- if (Input.GetMouseButtonDown(0))
- {
- _offset = transform.position - _direction;
- }
- }
- private void Move()
- {
- _rb2d.MovePosition(_target);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement