Skip to content

Instantly share code, notes, and snippets.

@manuelborst
Created August 2, 2021 15:55
Show Gist options
  • Save manuelborst/d220c13ce953cb9200311572c3ac26eb to your computer and use it in GitHub Desktop.
Save manuelborst/d220c13ce953cb9200311572c3ac26eb to your computer and use it in GitHub Desktop.
Drag and Drop 3D
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