250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- Unity
- rigidbody
- LayerMark
- Object Pooling
- Parallax
- Vector3
- Hp바
- 플레이어 방향전환
- Ainimation Blending
- Scrooling
- CSharp #자료구조
- Blend Type
- Hpbar
- joystick
- 2D슈팅게임
- Prefabs
- 패럴렉스
- apk
- 일시정지
- 유니티
- 스크롤링
- rotation
- Object Poling
- 프리팹
- Transform
- Animation State Machine
- raycast
- 플레이어 이동
- 오브젝트 풀링
- raycasting
Archives
- Today
- Total
주녘공부일지
[Unity3D] position (위치) // 플레이어 이동 예제 본문
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 |