-
-
Save davidhaley/6e43fcadbecae0b61b86fb4967fbbe92 to your computer and use it in GitHub Desktop.
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 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); | |
} | |
} | |
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 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; | |
} | |
} | |
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 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