2016. 3. 7. 18:54
https://blog.naver.com/nagne2011/220648267712
void Awake(){
Debug.Log ("Awake");
//객체 생성시 바로 실행
}
void OnEnable(){
Debug.Log ("onEnable");
//객체가 활성화 되면 실행
}
void Start (){
Debug.Log ("start");
//스크립트가 시작될때 실행
}
위의 순서대로 출력된다.
<Gizmos>
void OnDrawGizmos()
{
Gizmos.color = Color.red; //이하 모든 gizmos는 red로 색칠됨
//선그리기
Gizmos.DrawLine (ray.origin, ray.origin + ray.direction * distance);
//구 그리기
//Gizmos.DrawSphere (ray.origin, 0.1f);
//와이어 프레임
Gizmos.DrawWireSphere (ray.origin, 1.0f);
}
}
이것저것 그리는 함수.
Ray를 이용하면 해당 ray의 방향으로 선이 그어진다
Gizmos.color
Gizmos.color = Color.red; //이하 모든 gizmos는 red로 색칠됨
Gizmos.color = new Color(1.0f, 1.0f, 0.0f, 1.0f); //노란색
방법은 여러가지다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class RayCastTest : MonoBehaviour {
public float distance = 20.0f;
private Ray ray;
// Update is called once per frame
void Update () {
//ray의 기본위치
ray.origin = this.transform.position;
//ray의 방향
ray.direction = this.transform.forward;
}
void OnDrawGizmos()
{
Gizmos.color = Color.red; //이하 모든 gizmos는 red로 색칠됨
//선그리기
Gizmos.DrawLine (ray.origin, ray.origin + ray.direction * distance);
}
}
|
cs |
이렇게 하면 해당 RAY대로 선이 그어진다
Gizmos.DrawLine ("시작", "끝");
<RAY충돌확인>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
using UnityEngine;
using System.Collections;
public class RayCastTest : MonoBehaviour {
public float distance = 20.0f;
private Ray ray;
private RaycastHit rayinfo;
void Update () {
//ray의 기본위치
ray.origin = this.transform.position;
//ray의 방향
ray.direction = this.transform.forward;
//충돌 시 ture, 아니면 false
Debug.Log (Physics.Raycast (ray));
//만약 충돌시 rayinfo에 정보가 담긴다.
if (Physics.Raycast (ray, out rayinfo, distance, this.maskValue.value)) //특정 레이어만 지목할 수 있다.
{//시작점 부터 범위 안에까지 확인
Debug.Log (rayinfo.collider.gameObject);
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red; //이하 모든 gizmos는 red로 색칠됨
if(this.rayinfo.collider != null)
{
//충돌 지점 확인하기(rayhit)
Gizmos.DrawSphere (this.rayinfo.point, 0.1f);
//충돌시 튕기는 지점(rayhit)
Vector3 reflect = Vector3.Reflect(this.ray.direction,
this.rayinfo.normal);
Gizmos.color = new Color(1.0f, 1.0f, 0.0f, 1.0f); //노란색
Gizmos.DrawLine(this.rayinfo.point, this.rayinfo.point + reflect);
}
}
}
|
cs |
private RaycastHit rayinfo;
먼저 선언해둔다
위 physics를 이용하여 레이케스트의 충돌 판정을 내린다
//만약 충돌시 rayinfo에 정보가 담긴다.
Debug.Log (rayinfo.collider.gameObject);
//만약 rayinfo에 정보가 담기게 되면, Gizmos로 선을 그어준다.
충돌지점(rayinfo.point)에 Sphere을 하나 그려준다
반사각 함수
발사 방향과 그 대상의 노멀벡터를 입력하면 반사각도를 구해준다
Gizmos.DrawLine(this.rayinfo.point, this.rayinfo.point + reflect);
(충돌 지점엔 빨간 원, 반사된 선은 노란 선)
<범위 확인>
public class RayCastTest : MonoBehaviour {
public float distance = 20.0f;
private Ray ray;
private RaycastHit rayinfo;
void Update () {
//ray의 기본위치
ray.origin = this.transform.position;
//ray의 방향
ray.direction = this.transform.forward;
//충돌 시 ture, 아니면 false
Debug.Log (Physics.Raycast (ray));
//만약 충돌시 rayinfo에 정보가 담긴다.
if (Physics.Raycast (ray, out rayinfo, distance, this.maskValue.value)) //특정 레이어만 지목할 수 있다.
{//시작점 부터 범위 안에까지 확인
Debug.Log (rayinfo.collider.gameObject);
}
if(Input.GetButtonDown("Jump")){
//슈류탄 처럼 범위 내 판정
Collider[] targets = Physics.OverlapSphere (ray.origin, 1.0f);
//이하 범위안에 들어있는 항목이 배열로써 targets에 들어간다
foreach( Collider target in targets)
{
Debug.Log (target.gameObject);
}
}
}
}
|
cs |
슈류탄 같은 범위를 판정하는 함수
if(Input.GetButtonDown("Jump"))
무거우니까 제한을 둔다
Collider[] targets = Physics.OverlapSphere (ray.origin, 1.0f);
위치와 범위를 멤버변수로 넣어준다
targets에 배열로 감긴다
targets에서 부딪힌 것들을 target에 담아서 Log로 표시한다
<레이어 마스크>
(사진이 두개다)
우측에 해당 오브젝트의 Layer를 선택할 수 있다.
아래는 Ray의 Layer를 Target으로 바꾼것.
(원레는 타겟이될 오브젝트중 일부만 layer를 Target으로 바꾸는게 맞다)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
using UnityEngine;
using System.Collections;
public class RayCastTest : MonoBehaviour {
public float distance = 20.0f;
public LayerMask maskValue = -1;
//특정 레이어만 확인
private Ray ray;
private RaycastHit rayinfo;
void Update () {
//ray의 기본위치
ray.origin = this.transform.position;
//ray의 방향
ray.direction = this.transform.forward;
//만약 충돌시 rayinfo에 정보가 담긴다.
if (Physics.Raycast (ray, out rayinfo, distance, this.maskValue.value)) //특정 레이어만 지목할 수 있다.
{//시작점 부터 범위 안에까지 확인
Debug.Log (rayinfo.collider.gameObject);
}
//마스크의 이름을 통해서 해당 마스크를 불러 올 수도 있다.
//int layerTest = LayerMask.NameToLayer ("Target");
}
|
cs |
확인하기 위해 Layermask 변수를 선언하고, 비어있게 하기위해서 -1을 넣어준다
public으로 만들었기때문에 밖에서 target으로 담아줘야한다
다만 string으로 받는건 불안정하기 때문에,
private로 설정후 Awake() 한번만 해주는것이 좋다
if (Physics.Raycast (ray, out rayinfo, distance, this.maskValue.value)) //특정 레이어만 지목할 수 있다.
뒤에 this.maskValue.value 를 넣어주면 특정 레이어 중에서만 Ray를 확인한다
Physics.OverlapSphere (ray.origin, 1.0f, this.maskValue.value);
범위 채크할때 하는것도 특정 레이어만 지목 가능하다
기본 레이어는 아래처럼 바꿀 수 있다
tag를 추가하는 방식으로, 새로 추가 하거나 수정할 수 도 있다.
'유니티' 카테고리의 다른 글
유니티 Character Controller (0) | 2022.02.13 |
---|---|
유니티 2D 스프라이트 활용 (0) | 2022.02.13 |
유니티 튜토리얼 - TANKS! - 2 (0) | 2022.02.13 |
유니티 튜토리얼 - TANKS! - 1 (0) | 2022.02.13 |
유니티 물리처리 rigidbody (0) | 2022.02.13 |