'unity 바라보기'에 해당되는 글 1건

  1. 2019.12.05 12.05 유닛 바라보기 (LookAt 메서드 사용X) 1

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;
 
 
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);
            Debug.Log(angle);
            if (Vector3.Dot(model1.transform.right, model2.transform.position - model1.transform.position) >= 0f)
            {
                model1.transform.rotation = model1.transform.rotation * Quaternion.Euler(0, angle, 0);
            }
            else
            {
                model1.transform.rotation = model1.transform.rotation * Quaternion.Euler(0-angle, 0);
 
            }
 
 
        });
    }
 
    // Update is called once per frame
    void Update()
    {
        //Debug.Log(angle);
 
    }
}
 
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보다 큰지 작은지를 비교해 이를 해결했다.

: