Last active
April 4, 2018 03:19
-
-
Save kazukitanaka0611/5f4ddf930c7ecd805fb5a7a3f637e87b to your computer and use it in GitHub Desktop.
Mole.cs
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 Mole : MonoBehaviour { | |
[SerializeField] private float visibleHeight = 0.1f; | |
[SerializeField] private float hiddenHeight = -0.5f; | |
[SerializeField] private float speed = 4f; | |
[SerializeField] private float disappearDuration = 1.25f; | |
private Vector3 targetPosition = Vector3.zero; | |
private float disappearTimer = 0f; | |
/// <summary> | |
/// Awake is called when the script instance is being loaded. | |
/// </summary> | |
private void Awake() | |
{ | |
targetPosition = new Vector3(transform.localPosition.x, hiddenHeight, transform.localPosition.z); | |
transform.localPosition = targetPosition; | |
} | |
// Update is called once per frame | |
private void Update () | |
{ | |
disappearTimer -= Time.deltaTime; | |
if(disappearTimer <= 0) | |
{ | |
Hide(); | |
} | |
transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, Time.deltaTime * speed); | |
} | |
public void Rise() | |
{ | |
targetPosition = new Vector3(transform.localPosition.x, visibleHeight, transform.localPosition.z); | |
disappearTimer = disappearDuration; | |
} | |
public void OnHit() | |
{ | |
Hide(); | |
} | |
private void Hide() | |
{ | |
targetPosition = new Vector3(transform.localPosition.x, hiddenHeight, transform.localPosition.z); | |
} | |
} | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
public class GameController : MonoBehaviour { | |
[SerializeField] private GameObject moleContainer = null; | |
[SerializeField] private float spawnDuration = 1.5f; | |
[SerializeField] private float spawnDecrement = 0.1f; | |
[SerializeField] private float minimumSpawnDuration = 0.5f; | |
[SerializeField] private float gameTimer = 15f; | |
private Mole[] moles; | |
private float spawnTimer = 0f; | |
private float resetTimer = 3f; | |
// Use this for initialization | |
void Start () | |
{ | |
moles = moleContainer.GetComponentsInChildren<Mole>(); | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
gameTimer -= Time.deltaTime; | |
if(gameTimer > 0f) | |
{ | |
spawnTimer -= Time.deltaTime; | |
if(spawnTimer <= 0f) | |
{ | |
moles[Random.Range(0, moles.Length)].Rise(); | |
spawnDuration -= spawnDecrement; | |
if(spawnDuration < minimumSpawnDuration) | |
{ | |
Debug.Log("Speed up"); | |
spawnDuration = minimumSpawnDuration; | |
} | |
spawnTimer = spawnDuration; | |
} | |
} | |
else | |
{ | |
resetTimer -= Time.deltaTime; | |
if(resetTimer <= 0f) | |
{ | |
Debug.Log("restart"); | |
SceneManager.LoadScene(SceneManager.GetActiveScene().name); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment