using System.Collections; using System.Collections.Generic; using UnityEngine; public class myFirstCube : MonoBehaviour { public GameObject[] FindObjects; public GameObject player; private GameObject target; bool checkButton = false; // Use this for initialization void Start () { FindObjects = GameObject.FindGameObjectsWithTag("Respawn"); // 주변의 모든 "Respawn" 태그가 설정된 오브젝트를 찾습니다 player = GameObject.Find("Cube"); // 플레이어는 가운데 "Cube"오브젝트로 설정합니다. target = GetNearObject(player, FindObjects); // 첫 타겟을 설정합니다. } void Update () { if(Input.GetKey(KeyCode.A) && !checkButton) // 만약 입력된 키보드 값이 'A'버튼이라면, { Debug.Log("shoot the near ball!"); throwSphere(); } if(Input.GetKey(KeyCode.C)) { target = GetNearObject(player, FindObjects); Debug.Log("Find other ball :D. Next ball is " + target.name); } } void throwSphere() { target.transform.localPosition += Time.deltaTime * 15.0f * target.transform.up; checkButton = false; } GameObject GetNearObject(GameObject source, GameObject[] DestObjects) { GameObject near = DestObjects[0]; float shortDistance = Vector3.Distance(source.transform.position, DestObjects[0].transform.position); foreach(GameObject obj in DestObjects) { float distance = Vector3.Distance(source.transform.position, obj.transform.position); if(distance < shortDistance) { near = obj; shortDistance = distance; } } return near; } }