게임클라이언트 프로그래밍/Unity

3D Dot, 캐릭터의 시야

Game Client Lee Hwanguk 2023. 3. 2. 01:04

#Vector3.Dot

두 vector의 내적. 내적은 곱한 두 벡터의 크기와 그 사이 각도의 코사인을 곱한 
부동 소수점 값

https://docs.unity3d.com/ScriptReference/Vector3.Dot.html

 

Unity - Scripting API: Vector3.Dot

The dot product is a float value equal to the magnitudes of the two vectors multiplied together and then multiplied by the cosine of the angle between them. For normalized vectors Dot returns 1 if they point in exactly the same direction, -1 if they point

docs.unity3d.com

#Mathf 

https://docs.unity3d.com/kr/530/Manual/UnderstandingVectorArithmetic.html

 

벡터 연산 이해 - Unity 매뉴얼

벡터 연산은 3D 그래픽스, 물리 연산 및 애니메이션에 있어 핵심적이며 Unity를 최대한 활용하기 위해서는 벡터 연산을 깊이 이해하는 것이 유용합니다. 여기서는 주 연산 및 유용하게 사용할 수

docs.unity3d.com

#극좌표계

#4사분면?

#Player와 Monster

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestDotMain : MonoBehaviour
{   
    //내적 계산
    public Transform hero; 
    public Transform monster;

    public Transform pointL;
    public Transform pointR;

    //public Transform dir;

    private void Start()
    {
        //var cos = Mathf.Cos(30 * Mathf.Deg2Rad);
        //var sin = Mathf.Cos(30 * Mathf.Deg2Rad);
        //this.dir.position = new Vector3(cos, 0, sin);
        var pivot = 90;
        var sight = 45;

        var cos = Mathf.Cos((pivot + sight) * Mathf.Deg2Rad);
        var sin = Mathf.Sin((pivot + sight) * Mathf.Deg2Rad);
        this.pointL.position = new Vector3(cos, 0, sin);

        var cos2 = Mathf.Cos((pivot - sight) * Mathf.Deg2Rad);
        var sin2 = Mathf.Sin((pivot - sight) * Mathf.Deg2Rad);
        this.pointR.position = new Vector3(cos2, 0, sin);

    }
    //private float delta = 0f;
    private void Update()
    {
        //var toOther = Vector3.Normalize(this.monster.position - this.hero.transform.position);
        //DrawExtension.DrawArrow.ForDebug(this.hero.position, toOther, 100f, Color.cyan, DrawExtension.ArrowType.Solid);
        //var dot=Vector3.Dot(this.hero.transform.forward, toOther);
        //Debug.Log(dot);


        //monster의 회전(Player기준)
        //transform.RotateAround를 이용한 회전
        this.monster.transform.RotateAround(this.hero.transform.position,Vector3.up,20*Time.deltaTime); //point ,axis(회전축),angle

        //Cos,Sin을 이용한 회전
        //delta += Time.deltaTime*1;
        //var x = Mathf.Cos(delta);
        //var y = Mathf.Sin(delta);
        //this.monster.transform.position = new Vector3(x, 0, y);

        var toOther = Vector3.Normalize(this.monster.position - this.hero.position); //방향
        DrawExtension.DrawArrow.ForDebug(this.hero.position, toOther, 0f, Color.cyan, DrawExtension.ArrowType.Solid);
        var dot = Vector3.Dot(this.hero.transform.forward, toOther);
        Debug.Log(dot);
    }


}

#Updatae에서 dot를 출력하면서 확인