Skip to content

Instantly share code, notes, and snippets.

@ozergul
Created April 19, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozergul/af03e5bb9482636a13b65a01eab1cdac to your computer and use it in GitHub Desktop.
Save ozergul/af03e5bb9482636a13b65a01eab1cdac to your computer and use it in GitHub Desktop.
Unity 2D Dragger Script
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