Last active
December 6, 2018 23:14
-
-
Save TheCuttlefish/48d77ea6ed4cf3a2946344e9f63aec66 to your computer and use it in GitHub Desktop.
Drag and drop object (mouse to screen position)
This file contains hidden or 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.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