Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Last active December 6, 2018 23:14
Show Gist options
  • Save TheCuttlefish/48d77ea6ed4cf3a2946344e9f63aec66 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/48d77ea6ed4cf3a2946344e9f63aec66 to your computer and use it in GitHub Desktop.
Drag and drop object (mouse to screen position)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragAndDrop : MonoBehaviour {
bool active = false;
public Color selectedColour;
public Color idleColour;
void SetColour (Color c) {
GetComponent<Renderer> ().material.color = c;
}
void OnMouseDown () {
active = true;
transform.eulerAngles = Vector3.zero;
SetColour (selectedColour);
}
void OnMouseUp () {
active = false;
GetComponent<Rigidbody> ().velocity = Vector3.zero;
GetComponent<Rigidbody> ().angularVelocity = Vector3.zero;
SetColour (idleColour);
}
void Update () {
if (active) {
var v3 = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10f);
v3 = Camera.main.ScreenToWorldPoint (v3);
transform.position = v3;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment