Skip to content

Instantly share code, notes, and snippets.

/coin Secret

Created August 22, 2017 00:52
Show Gist options
  • Save anonymous/2597adf2c5aaa546884ab9c6c2472aa8 to your computer and use it in GitHub Desktop.
Save anonymous/2597adf2c5aaa546884ab9c6c2472aa8 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
//Create a reference to the CoinPoofPrefab
public GameObject coinpoof;
private static int coinsCollected = 0;
public int CoinsCollected
{
get
{
return coinsCollected;
}
set
{
coinsCollected = value;
}
}
public void OnCoinClicked()
{
Vector3 coinLocation = gameObject.transform.position;
// Instantiate the CoinPoof Prefab where this coin is located
// Make sure the poof animates vertically
Instantiate(coinpoof, new Vector3(coinLocation.x, coinLocation.y, coinLocation.z), Quaternion.Euler(-100f, 0, 0));
// Update the amount of Coins collected
CoinsCollected++;
// Destroy this coin. Check the Unity documentation on how to use Destroy
Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Door : MonoBehaviour
{
public AudioClip[] soundFiles;
public AudioSource soundSource;
// Create a boolean value called "locked" that can be checked in OnDoorClicked()
bool locked = true;
// Create a boolean value called "opening" that can be checked in Update()
bool opening = false;
private float fullHeight = 5.0f;
void Update() {
// If the door is opening and it is not fully raised
if (opening && transform.position.y < fullHeight)
{
// Animate the door raising up
transform.Translate(0, 2.5f * Time.deltaTime, 0);
}
}
public void OnDoorClicked() {
int doorLocked = 0;
int doorOpen = 1;
// If the door is clicked and unlocked
if (!locked)
// Set the "opening" boolean to true
// (optionally) Else
// Play a sound to indicate the door is locked
{
soundSource.clip = soundFiles[doorOpen];
soundSource.Play();
opening = true;
}
else
{
soundSource.clip = soundFiles[doorLocked];
soundSource.Play();
}
}
public void Unlock()
{
// You'll need to set "locked" to false here
locked = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Key : MonoBehaviour
{
//Create a reference to the KeyPoofPrefab and Door
public GameObject Door;
public GameObject Keypoof;
void Update()
{
//Not required, but for fun why not try adding a Key Floating Animation here :)
}
public void OnKeyClicked()
{
Vector3 keyLocation = gameObject.transform.position;
// Instatiate the KeyPoof Prefab where this key is located
// Make sure the poof animates vertically
Instantiate(Keypoof, new Vector3(keyLocation.x, keyLocation.y, keyLocation.z), transform.rotation);
// Call the Unlock() method on the Door
Door.GetComponent<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