Last active
April 2, 2018 07:02
-
-
Save kazukitanaka0611/3f3df87e9aca0acbae275c63211145c6 to your computer and use it in GitHub Desktop.
Unity whack a mole game 見てわかるUnityゲーム制作超入門 http://www.shuwasystem.co.jp/products/7980html/3849.html
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 delta = 0.01f; | |
[SerializeField] private float checkY = 1; | |
[SerializeField] private float startY = -1; | |
[SerializeField] private float timeDelta = 0; | |
private float lastTime = 0; | |
private bool gameFlg = true; | |
private int counter = 0; | |
private GameObject[,] moleData = new GameObject[5,5]; | |
private bool[,] flgData = new bool[5,5]; | |
// Use this for initialization | |
void Start () | |
{ | |
createMole(); | |
Random.InitState(System.Environment.TickCount); | |
lastTime = Time.time; | |
timeDelta = 1.2f; | |
gameFlg = true; | |
counter = 0; | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
if(!gameFlg) return; | |
randomSet(); | |
for(int i = 0; i < 5; i++) | |
{ | |
for(int j = 0; j < 5; j++) | |
{ | |
checkMole(i, j); | |
moveTo(i, j); | |
} | |
} | |
checkClicked(); | |
} | |
private void createMole() | |
{ | |
for(int i = 0; i < 5; i++) | |
{ | |
for(int j = 0; j < 5; j++) | |
{ | |
GameObject mole = GameObject.CreatePrimitive(PrimitiveType.Capsule); | |
mole.transform.Translate((i - 1) * 1.25f, startY, (j - 2) * 1.25f); | |
mole.GetComponent<Renderer>().material.color = new Color(0.75f, 0.2f, 0); | |
moleData[i, j] = mole; | |
flgData[i, j] = false; | |
} | |
} | |
} | |
private void randomSet() | |
{ | |
if((Time.time - lastTime) > timeDelta) | |
{ | |
int x = (int)Mathf.Floor(Random.value * 5); | |
int y = (int)Mathf.Floor(Random.value * 5); | |
flgData[x, y] = true; | |
lastTime = Time.time; | |
} | |
} | |
private void moveTo(int x, int y) | |
{ | |
if(!flgData[x, y]) return; | |
Vector3 pos = moleData[x, y].transform.position; | |
Vector3 toVector = new Vector3(pos.x, pos.y, pos.z); | |
toVector.y = checkY; | |
moleData[x, y].transform.position = Vector3.MoveTowards(pos, toVector, delta); | |
} | |
private void checkMole(int x, int y) | |
{ | |
if(moleData[x, y].transform.position.y == checkY) | |
{ | |
Vector3 pos = moleData[x, y].transform.position; | |
pos.y = startY; | |
moleData[x, y].transform.position = pos; | |
flgData[x, y] = false; | |
} | |
} | |
private void checkClicked() | |
{ | |
if(Input.GetMouseButtonDown(0)) | |
{ | |
GameObject obj = getClickObj(); | |
if(obj == null) return; | |
for(int i = 0; i < 5; i++) | |
{ | |
for(int j = 0; j < 5; j++) | |
{ | |
if(obj == moleData[i, j]) | |
{ | |
Vector3 pos = moleData[i, j].transform.position; | |
pos.y = startY; | |
moleData[i, j].transform.position = pos; | |
flgData[i, j] = false; | |
timeDelta -= 0.1f; | |
if(timeDelta < 0.1) timeDelta = 0.1f; | |
checkEnd(); | |
} | |
} | |
} | |
} | |
} | |
private GameObject getClickObj() | |
{ | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
if(Physics.Raycast(ray, out hit, 100)) | |
{ | |
return hit.collider.gameObject; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
private void checkEnd() | |
{ | |
counter++; | |
if(counter >= 20) | |
{ | |
gameFlg = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment