Skip to content

Instantly share code, notes, and snippets.

@davidhaley
Forked from anonymous/coin
Last active August 22, 2017 01:30
Show Gist options
  • Save davidhaley/6e43fcadbecae0b61b86fb4967fbbe92 to your computer and use it in GitHub Desktop.
Save davidhaley/6e43fcadbecae0b61b86fb4967fbbe92 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
public GameObject coinPoof;
private static int coinsCollected = 0;
public int CoinsCollected
{
get { return coinsCollected; }
set { coinsCollected = value; }
}
public void OnCoinClicked()
{
// Use gameObject.transform.position directly ( it is a Vector3! )
Instantiate(coinPoof, gameObject.transform.position, Quaternion.Euler(-100f, 0, 0));
CoinsCollected++;
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
public AudioClip[] soundFiles;
public AudioSource soundSource;
bool locked = true;
bool opening = false;
private float fullHeight = 5.0f;
void Update() {
if (opening && transform.position.y < fullHeight)
{
transform.Translate(0, 2.5f * Time.deltaTime, 0);
}
}
public void OnDoorClicked() {
if (!locked)
{
soundSource.clip = soundFiles[doorOpen];
soundSource.Play();
opening = true;
}
else
{
soundSource.clip = soundFiles[doorLocked];
soundSource.Play();
}
}
public void Unlock()
{
locked = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Key : MonoBehaviour
{
// Rather than GetComponent<Door>() below, just reference it directly
public Door Door;
public GameObject Keypoof;
void Update()
{
//Not required, but for fun why not try adding a Key Floating Animation here :)
}
public void OnKeyClicked()
{
// Use gameObject.transform.position directly ( it is a Vector3! )
Instantiate(Keypoof, gameObject.transform.position, transform.rotation);
// Call the Unlock() method on the Door
Door.Unlock();
// Destroy the key. Check the Unity documentation on how to use Destroy
Destroy(gameObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment