::目標たちを辿って宝箱を開けてみよう。
:2020-10-29
>>改善すべき:関数を分けるより、一つのログを作ってそれに従うようにしよう。
>>疑問点:キュー(Queue)を使ってみたが方向性が違った感じがする。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
// Start is called before the first frame update
Queue<GameObject> targets;
GameObject beforeTarget;
GameObject chestClosed;
GameObject chestOpened;
GameObject lastTarget;
void Start()
{
// :: Load Targets with name
targets = new Queue<GameObject>();
for(int i = 1; i <= 3; i++)
{
targets.Enqueue(GameObject.Find("Target" + i));
if(i == 3)
{
lastTarget = GameObject.Find("Target" + i);
}
}
Debug.Log(targets.Count);
// :: Initialise Chest
GameObject chestPrefabA = Resources.Load<GameObject>("chest_close");
GameObject chectPrefabB = Resources.Load<GameObject>("chest_open");
// :: Render Chest Closed and Set Position
chestClosed = Object.Instantiate<GameObject>(chestPrefabA);
chestClosed.transform.position = lastTarget.transform.position + new Vector3(0, 0f, 0.5f);
chestClosed.transform.eulerAngles = new Vector3(-90, -90, 0);
// :: Render Chest Open and Set Position and Set Active false;
chestOpened = Object.Instantiate<GameObject>(chectPrefabB);
chestOpened.transform.position = chestClosed.transform.position;
chestOpened.transform.eulerAngles = chestClosed.transform.eulerAngles;
chestOpened.SetActive(false);
// :: Initialise beforeTarget
beforeTarget = new GameObject();
// :: Button Load and Attach OnClick Function : Create
Button BUTTON_create = GameObject.Find("Button_Create").GetComponent<Button>();
BUTTON_create.onClick.AddListener(() => OnClick());
// :: Button Load and Attach OnClick Function : GoAndTurn
Button BUTTON_goAndTurn = GameObject.Find("Button_GoAndTurn").GetComponent<Button>();
BUTTON_goAndTurn.onClick.AddListener(() => { RunToTarget(); });
}
// :: Run to Target
void RunToTarget(float speed = 0.5f)
{
// :: If you have remain targets
if(targets.Count > 0)
{
// :: Go Target
beforeTarget = targets.Peek();
unit.GetComponent<Unit>().RunToTarget(targets.Dequeue(), speed);
}
}
// :: Go Target
void WalkToTarget(float speed = 0.5f)
{
// :: If you have remain targets
if (targets.Count > 0)
{
// :: Go Target
beforeTarget = targets.Peek();
unit.GetComponent<Unit>().WalkToTarget(targets.Dequeue(), speed);
}
}
// :: Go Complete
void GoComplete()
{
// :: Target Remains
if(targets.Count > 0)
{
// :: When arrived target1
if (targets.Peek().name.Contains("2"))
{
// :: Look at target forward
unit.GetComponent<Unit>().LookTarget(beforeTarget.transform.forward);
unit.GetComponent<Unit>().WaitNow();
}
else
{
// :: Walk To Target
this.WalkToTarget();
}
} else
{
Debug.Log("You've got treasure!");
chestClosed.SetActive(false);
chestOpened.SetActive(true);
}
}
// :: Wait Complete
void WaitComplete()
{
Debug.Log("wait is Over!");
// :: Walk To Target
this.WalkToTarget();
}
// :: OnClick Function
// ::: for Variables
GameObject unit; // :: Unit
void OnClick()
{
// :: Unit Resources Load & Instantiate
unit = Instantiate<GameObject>(Resources.Load<GameObject>("ch_03_01"));
unit.AddComponent<Unit>().goComplete = () => { GoComplete(); };
unit.GetComponent<Unit>().waitComplete = () => { WaitComplete(); };
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unit : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// :: Look at direction of Target
public void LookTarget(Vector3 direction)
{
this.gameObject.transform.LookAt(direction);
}
// :: Go and Turn with Target
// ::: for Variables
// :: Run To Target Function
// ::: for Variables
bool isGo = false;
string animationName_Run = "run@loop";
string animationName_Idle = "idle@loop";
GameObject target; // ::: target
float speed = 0.5f; // ::: Move Speed
public void RunToTarget(GameObject target, float speed = 0.5f)
{
// :: Remember Target
this.target = target;
// :: Remember Speed
this.speed = speed;
// :: Look at Target
this.LookTarget(this.target.transform.position);
// :: Animation Run
this.gameObject.GetComponent<Animation>().Play(animationName_Run);
// :: Start Update
isGo = true;
}
// :: Walk To Target Function
string animationName_Walk = "walk@loop";
public void WalkToTarget(GameObject target, float speed = 0.05f)
{
// :: Remember Target
this.target = target;
// :: Remember Speed
this.speed = speed;
// :: Look at Target
this.LookTarget(this.target.transform.position);
// :: Animation Run
this.gameObject.GetComponent<Animation>().Play(animationName_Walk);
// :: Start Update
isGo = true;
}
// :: Wait Function
float waitTime;
bool isWait = false;
float elapsedTime = 0f;
public CheckComplete waitComplete = () => { };
public void WaitNow(float waitTime = 3f)
{
// :: Remember Wait Time
this.waitTime = waitTime;
// :: Start Update
isWait = true;
}
// Update is called once per frame
// ::: for Variables
public delegate void CheckComplete();
public CheckComplete goComplete = () => { };
void Update()
{
// :: Start Go
if(isGo)
{
// :: Go Toward
this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position, this.target.transform.position, speed * Time.deltaTime);
// :: Check Distance
float distance = Vector3.Distance(this.gameObject.transform.position, this.target.transform.position);
// :: When near the Cube
if(distance <= 0.2f)
{
// :: Stop Go
isGo = false;
// :: Change Animation
this.gameObject.GetComponent<Animation>().Play(animationName_Idle);
// :: Send Move Complete
this.goComplete();
}
}
// :: Start Wait
else if(isWait)
{
// :: Check Elapsed Time
elapsedTime += Time.deltaTime;
Debug.Log(elapsedTime);
// :: When Wait is Over
if(elapsedTime >= waitTime)
{
// :: Stop Wait
isWait = false;
// :: Send Wait Complete
this.waitComplete();
}
}
}
}
'Unity' 카테고리의 다른 글
アカデミー#4:キー入力によって動く (0) | 2020.11.03 |
---|---|
アカデミー#3:武器を変える。 (0) | 2020.10.29 |
アカデミー#1:目標まで移動してヒット (0) | 2020.10.28 |
1 : UNITY=ANDROIDの記憶するべきもの (0) | 2020.10.27 |
0:Unity=Androidの問題と解決 (0) | 2020.10.21 |