<๋จ์ํ ์ํ์ข์ฐ>//๋ ํธ๋ก๋๋์ ํฝ์ ๊ฒ์์์ ๋ง์ด ๋ณด์ด๋ ๋ฌด๋น์ค๋ฝ๋ค
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MOVE : MonoBehaviour
{
Transform tr;
void Update()
{
tr = GetComponent<Transform>();
if (Input.GetKeyDown(KeyCode.UpArrow))
{
tr.position = new Vector3(tr.position.x, tr.position.y + 1, tr.position.z);
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
tr.position = new Vector3(tr.position.x, tr.position.y - 1, tr.position.z);
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
tr.position = new Vector3(tr.position.x -1, tr.position.y, tr.position.z);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
tr.position = new Vector3(tr.position.x + 1, tr.position.y, tr.position.z);
}
}
}
<translate ํจ์๋ฅผ ์ด์ฉํ ์ํ์ข์ฐ์ด๋>//๋ชจ๋ฐ์ผ ๊ฒ์ ์์ ์ ๋ฌด๋น๊ณผ ๋น์ทํ๋ค๊ณ ๋๋.
But.. Input.GetAxis๋ ํค๋ณด๋์์๋ง ๋จนํ๋ ๋ช ๋ น์ด๋ผ์ ํฐ์น(ํด๋ฆญ)๊ธฐ๋ฐ์ ๋ชจ๋ฐ์ผ์์๋ ์ ๋จนํ๋ค.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MOVE : MonoBehaviour
{
Transform tr;
void Update()
{
tr = GetComponent<Transform>();
float x = Input.GetAxis("Horizontal")*0.05f;
float y= Input.GetAxis("Vertical") * 0.05f;
tr.Translate(new Vector2(x, y));
}
}
์์ ํจ์์์๋ ๊ฐ๊ฐ x์ y๋ณ์์ Input.GetAxis() ํจ์๋ฅผ ํตํด ๊ฐ๋ก์ถ๊ณผ ์ธ๋ก์ถ์ ๊ฐ์ ๊ฐ์ ธ์จ๋ค.
(๊ฒ์๊ธฐ ๋ฑ์ ์ด๋์ ์ฌ์ฉํ๋ ์๋ ๋ก๊ทธ ์คํฑ์์ ๊ฐ์ ธ์จ ๊ฐ๋
)
Translate() ํจ์๋ ์คํฌ๋ฆฝํธ๊ฐ ๋ค์ด์๋ ๊ฒ์ ์ค๋ธ์ ํธ์ ์์น์์ Translate() ํจ์์ ์ ๋ฌ๋ฐ์ ๊ฐ์ ํ์ฌ ์ขํ ๊ฐ์ ๋ํ๋๋ก ํด์ฃผ๋ ํจ์์ด๋ค.
Translate() ํจ์์ transform.position์ ์ฐจ์ด์
์ผ๋จ ๋์ ๋๋ ์ฐจ์ด์ ์ Translate๋ ํจ์ ๊ทธ์์ฒด๋ผ์ ํจ์ ์์ Vector๊ฐ์ ์ธ์๋ก ๋ฃ๋ ์์ด๊ณ
transform.position์ Vector๊ฐ์ ๋์ ํด์ฃผ์ด์ผ ํ๋ค.
๋ํ
Translate๋ ์ค๋ธ์ ํธ ์์ ์ ๊ธฐ์ค์ผ๋ก ์๋์ ์ผ๋ก ์ง์ ํ ์ขํ๋ก ํฅํ๋ ์ ์ง์ ์ธ ์์ง์์ ๋ง๋ค์ด๋ด๊ณ
transform.position์ ์ขํ์ ์ ๋ ์์น๋ณ๋ก ๋จ๋ ๊ฐ์ด๋ฏ๋ก ๋ฐ๋ก ์ง์ ํ ์ขํ๋ก ๋ํ๋๊ฒ ํ๋ค.
์ ์๊ฐ ๋์ ์ธ ์ด๋ ๊ฐ๋ ์ด๋ผ๋ฉด ํ์๋ ์ ์ ์ธ ํ ๋ ํฌํธ๊ฐ์ ๋๋์ด๋ผ๊ณ ์ดํดํ๋ฉด ์ฝ๋ค.
'Unity > Unity(๊ธฐ์ด)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Unity์์ this์ ์๋ฏธ (0) | 2021.08.17 |
---|---|
Unity ๋์ ์์ฑ์ Random.Range๋ฅผ ํ์ฉํ์ (0) | 2021.08.17 |
์ ๋ํฐ Quaternion (0) | 2021.06.05 |
[์ ๋ํฐ] get set ํ๋กํผํฐ Property(์์ฑ) (0) | 2021.05.23 |
[์ ๋ํฐ] Debug.Log์ Debug.LogFormat ํ์ฉ. ์ด๋ค ๊ฒ์ ์จ์ผํ ๊น? (0) | 2021.05.22 |