12.05 유닛 바라보기 (LookAt 메서드 사용X)
C#/실습 2019. 12. 5. 17:28코드:
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App2 : MonoBehaviour
{
public GameObject model1;
public GameObject model2;
public Button btn;
float angle;
// Start is called before the first frame update
void Start()
{
btn.onClick.AddListener(() =>
{
angle = Vector3.Angle(model2.transform.position - model1.transform.position, model1.transform.forward);
if (Vector3.Dot(model1.transform.right, model2.transform.position - model1.transform.position) >= 0f)
{
}
else
{
}
});
}
// Update is called once per frame
void Update()
{
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
그냥 A 와 B사이의 벡터를 구하고 A가 바라보는 벡터의 각을 구하기엔 B가 A의 오른쪽에 있든 왼쪽에 있든 같은 angle값이 나오기때문에 구분이 불가능했다.
따라서 A의 right 벡터와 A,B사이 벡터의 내적을 구하고 이것이 0보다 큰지 작은지를 비교해 이를 해결했다.
'C# > 실습' 카테고리의 다른 글
12.09 충돌감지와 sprite활용하기(delegate) (0) | 2019.12.09 |
---|---|
12.06 드래곤플라이트 구현 (0) | 2019.12.06 |
11.29 캐릭터 움직이기 (Coroutine, Update 비교 및 활용) (0) | 2019.11.29 |
11.26 Hero와 Monster의 App에 의한 수동적 싸움 (delegate) (0) | 2019.11.26 |
11.13 unity 유닛 동적생성, 유닛 이동 및 사정거리 내 진입시 공격하기 (0) | 2019.11.13 |