Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TheCuttlefish/98396b6098d8b831885cb4eb66aa7f5d to your computer and use it in GitHub Desktop.
Save TheCuttlefish/98396b6098d8b831885cb4eb66aa7f5d to your computer and use it in GitHub Desktop.
Drag and Drop with wall avoidance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragAndDropBoundaries : MonoBehaviour {
bool active = false;
public Color selectedColour;
public Color idleColour;
public float velocityMulitplier = 2f;
Color newColour = Color.white;
void SetColour (Color c) {
newColour = c;
//GetComponent<Renderer> ().material.color = c;
}
void OnMouseDown () {
active = true;
transform.eulerAngles = Vector3.zero;
SetColour (selectedColour);
}
void OnMouseUp () {
active = false;
SetColour (idleColour);
}
void ColourTransition () {
GetComponent<Renderer> ().material.color = Color.Lerp (GetComponent<Renderer> ().material.color, newColour, 2 * Time.deltaTime);
}
void FixedUpdate () {
ColourTransition ();
if (active) {
var v3 = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10f);
v3 = Camera.main.ScreenToWorldPoint (v3);
GetComponent<Rigidbody> ().velocity = (v3 - transform.position) * velocityMulitplier;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment