Newer
Older
MI-AudioMixer / Assets / Scripts / DraggableObject.cs
@flameshadow flameshadow on 10 Apr 2019 1 KB first commit
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DraggableObject : MonoBehaviour
{
    private Vector3 offset;
    private Rigidbody2D rb;
    private float dragSpeed = 25f;
    private void OnMouseDown()
    {
        Vector3 currScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(currScreenPoint);
        GetComponent<Renderer>().sortingLayerName = "Foreground";
        rb.freezeRotation = false;
    } // void OnMouseDOwn()

    private void OnMouseDrag()
    {
        Vector3 currScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
        Vector3 currPosition = Camera.main.ScreenToWorldPoint(currScreenPoint) + offset;
        transform.position = currPosition;
        //Vector2 target = currPosition - transform.position;
        //rb.velocity = target * dragSpeed;
    } // void OnMouseDraw()

    private void OnMouseUp()
    {
        GetComponent<Renderer>().sortingLayerName = "Default";
        //GetComponent<Renderer>().sortingOrder = 9;
        rb.velocity = Vector2.zero;
        rb.freezeRotation = true;
        //rb.gravityScale = 1;
    }
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}