Created
April 19, 2020 18:38
-
-
Save ozergul/af03e5bb9482636a13b65a01eab1cdac to your computer and use it in GitHub Desktop.
Unity 2D Dragger Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using DG.Tweening; | |
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.EventSystems; | |
public class Dragger : MonoBehaviour, IDragHandler | |
{ | |
public GameObject target; | |
public bool dragable = true; | |
public float smoothTime = 0.1F; | |
private Vector2 velocity = Vector2.zero; | |
public UnityEvent afterCollide; | |
public void OnDrag(PointerEventData eventData) | |
{ | |
if (dragable) | |
{ | |
var mousePosition = Input.mousePosition; | |
transform.position = Vector2.SmoothDamp(transform.position, | |
new Vector2(mousePosition.x, mousePosition.y), ref velocity, smoothTime); | |
} | |
} | |
void OnTriggerEnter2D(Collider2D col) | |
{ | |
if (col.transform.name == target.name) | |
{ | |
afterCollide.Invoke(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment