Created
April 20, 2019 00:16
-
-
Save gkagm2/e9016d150a8e7de5ebfaa7461be965d2 to your computer and use it in GitHub Desktop.
Uselful API(Distance, Random, FindChild
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 csDistance : MonoBehaviour { | |
//거리를 구하기 위한 게임 오브젝트들의 트랜스폼을 저장할 변수 선언. | |
public Transform box1; | |
public Transform box2; | |
public Transform box3; | |
// Use this for initialization | |
void Start () { | |
//두 Object 사이의 거리 구하기1 | |
float distance1 = Vector3.Distance(transform.position, box2.position); //두 게임 오브젝트 사이의 거리를 구할 수 있는 메서드다. | |
Debug.Log("distance1 : " + distance1); | |
//두 Object 사이의 거리 구하기2 | |
//타겟이 항상 앞에 나와야 한다. | |
float distance2 = (box3.position - transform.position).magnitude; // 두 게임 오브젝트 사이의 거리를 구할 수 있는 또 다른 메서드다. | |
Debug.Log("distance2 : " + distance2); | |
//방향 구하기 | |
Vector3 dir = box2.position - transform.position; | |
Debug.Log("before dir : " + dir); | |
dir.Normalize(); //방향은 거리가 필요 없으므로 0~1 사이의 값으로 단위화해서 표시하기 때문에 방향을 구하기 위한 벡터를 구해서 단위화한다. | |
Debug.Log("after dir : " + dir.normalized); | |
//회전 | |
transform.eulerAngles = dir; //앞에서 구한 방향으로 회전시킨다. | |
} | |
private void Update() | |
{ | |
//방향 구하기 | |
Vector3 dir = box2.position - transform.position; | |
Debug.Log("before dir : " + dir); | |
dir.Normalize(); //방향은 거리가 필요 없으므로 0~1 사이의 값으로 단위화해서 표시하기 때문에 방향을 구하기 위한 벡터를 구해서 단위화한다. | |
Debug.Log("after dir : " + dir.normalized); | |
//회전 | |
transform.eulerAngles = dir; //앞에서 구한 방향으로 회전시킨다. | |
} | |
} |
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 csFindChild : MonoBehaviour { | |
public Transform[] myChilds; | |
// Use this for initialization | |
void Start () { | |
int randomSeeds; | |
randomSeeds = (int)System.DateTime.Now.Ticks; | |
Random.seed = randomSeeds; | |
//SpawnPoint 게임 오브젝트를 찾는다. 그다음 자식 객체들의 트랜스폼을 가져와 배열에 담는다. | |
myChilds = GameObject.Find("SpawnPoint").GetComponentsInChildren<Transform>(); | |
//찾아온 자식 게임 오브젝트의 개수로 숫자의 범위를 정한다. | |
int idx = Random.Range(1, myChilds.Length); | |
//범위 안의 정수를 구해 출력한다. | |
Debug.Log("SpawnPoint.Length : " + myChilds.Length); | |
Debug.Log("Random.Range : " + idx); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
} |
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 csRandom : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
//랜덤값 구하기 | |
int randomSeeds; | |
randomSeeds = (int)System.DateTime.Now.Ticks; //난수를 구하기 위한 시드값으로 현재 시각을 이용한다. | |
Random.seed = randomSeeds; // 시드값을 설정함. | |
//Random.Range(이상, 미만) | |
int randomX = Random.Range(5, 10); //범위 안에 정수를 구함 | |
Debug.Log("integer : " + randomX); | |
float randomY = Random.Range(5.0f, 10.0f); //범위 안에 실수를 구함 | |
Debug.Log("float : " + randomY); | |
//범위 제한 | |
//현재값이 최댓값보다 크면 그 최댓값까지만 출력해주고 | |
//최솟값보다 작으면 그 최솟값으로만 출력 | |
int myVal = 10; | |
float nLimit = Mathf.Clamp(myVal, 1, 5); //수의 범위를 제한한다. 즉, 현재값이 최댓값보다 크면 최댓값을 반환하고, 현재값이 최솟값보다 작으면 최솟값을 반환한다. | |
Debug.Log("Clamps : " + nLimit); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment