주녘공부일지

[Unity3D] position (위치) // 플레이어 이동 예제 본문

GameEngine/UnityEngine - Class

[Unity3D] position (위치) // 플레이어 이동 예제

주녘 2022. 2. 21. 16:48
728x90

1. 3D 플레이어 이동 구현 ( position )

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

public class PlayerController : MonoBehaviour
{
    [SerializeField] float _speed = 10.0f;

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
            transform.position += new Vector3(0f, 0f, 1f) * Time.deltaTime * _speed;
        if (Input.GetKey(KeyCode.S))
            transform.position -= new Vector3(0f, 0f, 1f) * Time.deltaTime * _speed;
        if (Input.GetKey(KeyCode.A))
            transform.position += new Vector3(1f, 0f, 0f) * Time.deltaTime * _speed;
        if (Input.GetKey(KeyCode.D))
            transform.position -= new Vector3(1f, 0f, 1f) * Time.deltaTime * _speed;
     }
}

https://docs.unity3d.com/kr/530/ScriptReference/Input.html

 

Unity - 스크립팅 API: Input

Input Manager에 설정된 각 축을 읽고 모바일 장치의 멀티터치/가속도계 데이터에 접근을 하는 경우에 이 클래스를 사용합니다. 다음의 기본 설정 축과 Input.GetAxis 함수를 이용해서 해당 축을 읽습니

docs.unity3d.com

 

 

2. 예약어 사용 ( Vector3(0f, 0f, 1f) == Vector3.forward ) // 가독성↑

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

public class PlayerController : MonoBehaviour
{
    [SerializeField] float _speed = 10.0f;

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
            transform.position += Vector3.forward * Time.deltaTime * _speed;
        if (Input.GetKey(KeyCode.S))
            transform.position += Vector3.back * Time.deltaTime * _speed;
        if (Input.GetKey(KeyCode.A))
            transform.position += Vector3.left * Time.deltaTime * _speed;
        if (Input.GetKey(KeyCode.D))
            transform.position += Vector3.right * Time.deltaTime * _speed;
    }
}

 

 

3. 로컬 좌표를 월드 좌표로 변환 ( TransformDirection ) // Local -> World

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

public class PlayerController : MonoBehaviour
{
    [SerializeField] float _speed = 10.0f;

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
            transform.position += transform.TransformDirection(Vector3.forward * Time.deltaTime * _speed);
        if (Input.GetKey(KeyCode.S))
            transform.position += transform.TransformDirection(Vector3.back * Time.deltaTime * _speed);
        if (Input.GetKey(KeyCode.A))
            transform.position += transform.TransformDirection(Vector3.left * Time.deltaTime * _speed);
        if (Input.GetKey(KeyCode.D))
            transform.position += transform.TransformDirection(Vector3.right * Time.deltaTime * _speed);
    }
}

 

+ 월드 좌표를 로컬 좌표로 변환 ( InverseTransformDirection ) // World -> Local

 

 

4. Translate ( position 변경 함수 : 로컬 좌표계를 기준으로 함 ) // 가독성↑

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

public class PlayerController : MonoBehaviour
{
    [SerializeField] float _speed = 10.0f;

    void Update()
    {
        if (Input.GetKey(KeyCode.W))
            transform.Translate(Vector3.forward * Time.deltaTime * _speed);
        if (Input.GetKey(KeyCode.S))
            transform.Translate(Vector3.back * Time.deltaTime * _speed);
        if (Input.GetKey(KeyCode.A))
            transform.Translate(Vector3.left * Time.deltaTime * _speed);
        if (Input.GetKey(KeyCode.D))
            transform.Translate(Vector3.right * Time.deltaTime * _speed);
    }
}
728x90

'GameEngine > UnityEngine - Class' 카테고리의 다른 글

[Unity3D] Rotation (회전)  (0) 2022.02.21
[Unity3D] Vector3  (0) 2022.02.21
[Unity] 모바일 터치 감지 방법  (0) 2022.02.16
[Unity] ForceMode(ForceMode2D)  (0) 2022.01.26
[Unity] Prefab이란? (+ 불러오기)  (0) 2022.01.25