Skip to content

Instantly share code, notes, and snippets.

@bibinba
Created May 3, 2020 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bibinba/359cec943c7b0f7f9d5519ccd1110b56 to your computer and use it in GitHub Desktop.
Save bibinba/359cec943c7b0f7f9d5519ccd1110b56 to your computer and use it in GitHub Desktop.
Unity1Week:お題「蜜」で使ったスクリプト。rayが当たったら特定のタグのものが消えてポイント加算
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GamaManager : MonoBehaviour
{
public List<GameObject> Flower = new List<GameObject>();
private RaycastHit hit;
private int point=0;
public Text Text_Point;
public GameObject EndCanvas;
public GameObject StartCanvas;
public AudioSource Click;
void Start()
{
StartCanvas.SetActive(true);
EndCanvas.SetActive(false);
CreateFlower();
}
void Update()
{
Text_Point.text = point.ToString() + "/5";
if (!StartCanvas.activeSelf)
{
if (Input.GetMouseButtonDown(0)) //マウスがクリックされたら
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //マウスのポジションを取得してRayに代入
if (Physics.Raycast(ray, out hit)) //マウスのポジションからRayを投げて何かに当たったらhitに入れる
{
if (hit.collider.gameObject.tag == "Flower")
{
point++;
Destroy(hit.collider.gameObject);
Click.Play();
Debug.Log("Hit");
}
}
}
}
if (point == 5)
{
GameClear();
}
}
private void GameClear()
{
Debug.Log("GameClear!");
EndCanvas.SetActive(true);
}
public void Restart()
{
SceneManager.LoadScene("Main");
}
private void CreateFlower()
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
GameObject flower = Instantiate(Flower[i]);
flower.transform.position = new Vector3(Random.Range(4, -5), Random.Range(4, -5), 0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment