Created
December 7, 2018 01:19
-
-
Save TheCuttlefish/98396b6098d8b831885cb4eb66aa7f5d to your computer and use it in GitHub Desktop.
Drag and Drop with wall avoidance
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 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