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 |
Tags
- C#
- Hp바
- 깊이 우선 탐색
- pccp 기출문제 1번
- 플레이어 이동
- pccp 기출문제 2번
- LayerMark
- CSharp #자료구조
- 2D슈팅게임
- Ainimation Blending
- Scrooling
- Hpbar
- raycasting
- 프로그래머스
- 유니티
- ASTAR
- Object Pooling
- Unity
- 오브젝트 풀링
- 너비 우선 탐색
- Blend Type
- Algorithm
- Animation State Machine
- heap tree
- Back Tracking
- Lv2
- 플레이어 방향전환
- Object Poling
- pccp 기출문제 3번
- 충돌위험 찾기
Archives
- Today
- Total
주녘공부일지
[Unity3D] position (위치) // 플레이어 이동 예제 본문
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
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);
}
}
'GameDevelopment > [Unity] 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 |