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 이동
}
}
방향벡터 노멀라이즈 해서 사용
방향 벡터 - 벡터의 정규화(normalized) 유니티
더보기 "유니티 방향 벡터" "유니티 노말라이즈드" 오브젝트 균일한 이동을 위하여 벡터의 정규화가 필요합니다. 그 이유는 모든 방향의 벡터 길이가 1 이어야 방향에 따른 이동 속도가 같아지기
seojingames.tistory.com
'Unity > 참고자료' 카테고리의 다른 글
| Unity : 유니티 라이프사이클(가장 중요) (0) | 2021.10.10 |
|---|---|
| Unity : 스크립트를 실행하는 방법 (0) | 2021.10.10 |
| Unity : 인터페이스 및 유니티 메뉴얼 (0) | 2021.10.10 |
| Unity : 유니티 기초, 문법 , 게임 설계 (0) | 2021.10.06 |
| 월드 좌표계와 로컬 좌표계 (0) | 2021.10.05 |