본문 바로가기
작업

유니티 공부 C# Vector 클래스

by SOGUL 2023. 7. 16.

* vector 클래스는 캐릭터를 움직일 때 자주 쓴다

 

 

class Vector3

{

   public float x;

   public float y;

   public float z;

 

}

 

* 좌표나 벡터로 쓸 수 있다

* x=8 y=2 일 때, 좌표로 쓰면 오브젝트가 (8,2) 에 배치되었다는 의미

* 벡터로 쓰면 현재 위치에서 x축 방향으로 8 y축 방향으로 2 움직였다는 의미

 

 

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

public class Test : MonoBehaviour 
{

    void Start()
    {
        Vector2 playerPos = new Vector2(3.0f, 6.0f); //vector2 클래스의 playerPos 변수 선언 = 인스턴스 작성해 대입
        playerPos.x += 8.0f;
        playerPos.y += 5.0f;
        Debug.Log(playerPos);
    }


}

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

public class Test : MonoBehaviour 
{

    void Start() // startPos 에서 endPos로 향하는 dir 벹거 구하기
    {
        Vector2 startPos = new Vector2(3.0f, 1.0f);
        Vector2 endPos = new Vector2(8.0f, 5.0f);
        Vector2 dir = endPos - startPos;
        Debug.Log(dir);

        float len = dir.magnitude; // startPos부터 endPos까지의 거리를 구함. 이 거리는 dir벡터의 길이와 같음
        Debug.Log(len);
    }


}

 

* magnitude 

 

* vector 클래스는 가속도, 힘, 이동속도 같은 물리 수치로 사용 가능하다

 

'작업' 카테고리의 다른 글

유니티 빌드 오류  (1) 2023.07.16
유니티 Hub 모듈 설치 오류 문제  (0) 2023.07.16
유니티 공부 C# 클래스  (0) 2023.07.16
유니티 공부 C# 메서드  (0) 2023.07.13
유니티 공부 C# 배열  (0) 2023.07.13