Unity/참고자료

Vector

치명적흑형 2021. 10. 4. 16:09
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //Vector2 playerPos = new Vector2(3, 4);
        //playerPos.x += 8;
        //playerPos.y += 5;
        //Debug.Log(playerPos);

        Vector2 startPos = new Vector2(2, 1);
        Vector2 endPos = new Vector2(8, 5);
        //Vector2 dir = endPos - startPos;  //방향벡터
        Vector2 dir = startPos - endPos;    //방향벡터, 방향이 반대, 길이는 동일
        Debug.Log(dir); //방향벡터

        float len = dir.magnitude;  //벡터의 길이 (두 벡터의 거리를 측정하기 위함)

        Vector2 nomal = dir.normalized; //벡터의 정규화, 길이가 1인 벡터 (단위벡터)
        Debug.Log(len);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

위치벡터 - 위치벡터 = 방향벡터

 

방향벡터의 길이는 .magnitude 로 구할수 있다.

 


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

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //Vector2 playerPos = new Vector2(3, 4);
        //playerPos.x += 8;
        //playerPos.y += 5;
        //Debug.Log(playerPos);

        Vector2 startPos = new Vector2(2, 1);
        Vector2 endPos = new Vector2(8, 5);
        Vector2 dir = endPos - startPos;
        Debug.Log(dir); //방향벡터

        float len = dir.magnitude;  //벡터의 길이 (두 벡터의 거리를 측정하기 위함)
        Debug.Log(len);

        Vector2 nomal = dir.normalized; //벡터의 정규화, 길이가 1인 벡터 (단위벡터)
        Debug.Log(nomal);

        Vector2 extends = dir * 3;
        Debug.Log(extends);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

큐브의 이동

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

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //Test스크립트가 붙어있는 게임오브젝트의 position.z측으로 0.03만큼 매 프레임마다 이동
        //this.gameObject.transform.position += new Vector3(0, 0, 0.03f);
        this.transform.position += new Vector3(0, 0, 0.03f);
    }
}

 

 

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

public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //this.transform.position += new Vector3(0, 0, 0.03f); //z축 0.03 이동
        //this.transform.position -= new Vector3(0, 0, 0.03f); //z축 0.03 뒤로 이동
        //this.transform.position += new Vector3(0.03f, 0, 0); //x축 0.03 이동
        this.transform.position += new Vector3(0, 0.03f, 0); //y축 0.03 이동
    }
}

 

 

 

방향벡터 노멀라이즈 해서 사용

https://seojingames.tistory.com/entry/%EB%B0%A9%ED%96%A5-%EB%B2%A1%ED%84%B0-%EB%B2%A1%ED%84%B0%EC%9D%98-%EC%A0%95%EA%B7%9C%ED%99%94normalized-%EC%9C%A0%EB%8B%88%ED%8B%B0

 

방향 벡터 - 벡터의 정규화(normalized) 유니티

더보기 "유니티 방향 벡터" "유니티 노말라이즈드" 오브젝트 균일한 이동을 위하여 벡터의 정규화가 필요합니다. 그 이유는 모든 방향의 벡터 길이가 1 이어야 방향에 따른 이동 속도가 같아지기

seojingames.tistory.com