Skip to content

Instantly share code, notes, and snippets.

@ozergul

ozergul/Dragger.cs

Created Apr 19, 2020
Embed
What would you like to do?
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