Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using UnityEngine;
using System.Collections;
public class Loot_Customizer : MonoBehaviour {
//--------------------PICKUP MANAGER--------------------//
//By default, finds GameObjects with the tag "Player"
public bool detectPlayerTag = true;
//Manually assign the player otherwise
public GameObject assignPlayer;
//Declares player and playerDistance for later assignment
GameObject player;
float playerDistance;
//Loot vanishes when the player comes in contact with it
public bool pickUpOnTouch = true;
//Loot vanishes when the player is nearby and presses the command key
public KeyCode pickupCommand;
public float pickupDistance = 1.0f;
//--------------------ANIMATION MANAGER--------------------//
//Pops the loot out of the chest in a charming and slightly randomized arch
public bool lootAnimationCurves = true;
//Create animation curve and randomization
public AnimationCurve animationCurve;
public float animationLength = 0.5f;
public float curveHeight = 1.0f;
public float curveHeightVariation = 0.5f;
public float curveLength = 2.0f;
public float curveLengthVariation = 0.5f;
public float arcAngle = 60.0f;
//Runs animations at Start
IEnumerator lootAnimation()
float elapsedTime = 0.0f;
//Randomizers for curve height, length and angle
curveHeight = Random.Range ((curveHeight), (curveHeight + curveHeightVariation));
curveLength = Random.Range ((curveLength), (curveLength + curveLengthVariation));
float randomRotY = Random.Range (-arcAngle, arcAngle);
//Store parent transforms
Vector3 parent_pos = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
Vector3 parent_rot = new Vector3 (0, 0, 0);
Vector3 parent_scale = new Vector3 (1, 1, 1);
Vector3 parent_pos = new Vector3 (0, 0, 0);
Vector3 parent_rot = new Vector3 (0, 0, 0);
Vector3 parent_scale = new Vector3 (1, 1, 1);
if (transform.parent != null) {
parent_pos = transform.parent.localPosition;
parent_rot = transform.parent.localEulerAngles;
parent_scale = transform.parent.localScale;
//Set random rotation of curve if relevant
if (arcAngle != 0)
parent_rot.y = randomRotY;
Quaternion quat = Quaternion.Euler (parent_rot);
Matrix4x4 space = Matrix4x4.TRS (parent_pos, quat, parent_scale);
Vector3 start_position = transform.localPosition;
while (elapsedTime < animationLength)
//Generate position on Y axis
float lootPosY = animationCurve.Evaluate(elapsedTime / animationLength) * curveHeight;
Vector3 next_pos = new Vector3 ((start_position.x + curveLength * elapsedTime), lootPosY , 0);
transform.localPosition = space * next_pos;
yield return null;
//Increment time towards ending the loop
elapsedTime += Time.deltaTime;
//--------------------RUNTIME FUNCTIONS--------------------//
// Use this for initialization
void Start ()
//Sets the player variable or throws a warning
if (detectPlayerTag)
player = GameObject.FindGameObjectWithTag ("Player");
} else if (assignPlayer != null)
player = assignPlayer;
} else {
Debug.LogError ("No valid player assigned! Loot will be non-interactable and will generate errors. Please either \"Automically Detech Player\" if you are using the \"Player\" tag, or assign your playable character's GameObject to \"Assign Player\".");
//Checks to make sure loot can be picked up
if (!pickUpOnTouch && pickupCommand == KeyCode.None)
Debug.LogError ("There is no way for the player to pick up loot! Please either check \"Pick Up On Touch\" or assign a \"Pick Up Command\".");
//Run animation if requested
if (lootAnimationCurves)
StartCoroutine ("lootAnimation");
void Update()
//Calculates player's distance from the chest
playerDistance = Vector3.Distance (gameObject.transform.position, player.transform.position);
//Pick up loot
if ((Input.GetKeyDown (pickupCommand) && !pickUpOnTouch) || pickUpOnTouch)
//Check if the player is close enough to open the chest
if (playerDistance <= pickupDistance)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment