Created
August 2, 2021 15:55
-
-
Save manuelborst/d220c13ce953cb9200311572c3ac26eb to your computer and use it in GitHub Desktop.
Drag and Drop 3D
This file contains 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; | |
class DragTransform : MonoBehaviour | |
{ | |
private Color mouseOverColor = Color.blue; | |
private Color originalColor = Color.yellow; | |
private bool dragging = false; | |
private float distance; | |
void OnMouseEnter() | |
{ | |
GetComponent<Renderer>().material.color = mouseOverColor; | |
} | |
void OnMouseExit() | |
{ | |
GetComponent<Renderer>().material.color = originalColor; | |
} | |
void OnMouseDown() | |
{ | |
distance = Vector3.Distance(transform.position, Camera.main.transform.position); | |
dragging = true; | |
} | |
void OnMouseUp() | |
{ | |
dragging = false; | |
} | |
void Update() | |
{ | |
if (dragging) | |
{ | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
Vector3 rayPoint = ray.GetPoint(distance); | |
transform.position = rayPoint; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment