| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
- 큐
- Queue
- 공부
- std::unordered_map
- 스택영역
- 알고리즘
- 차이점
- MonoBehaviour
- map
- 트리
- 배열
- 객체지향
- 힙영역
- 기술면접
- 멀티쓰레드
- rookiss
- list
- 해쉬맵
- 자료구조
- c#
- 반복문
- 스택
- std::map
- static_cast
- 벡터
- thread
- 프래그멘테이션
- 인프런
- 리스트
- vector
- Today
- Total
호빵의 IT 개발소
[MMO RPG] Rotation 본문
한 방향으로 회전 구현(2가지)
1. 절대 회전값
-eulerAngle 회전을 구현할 때는 연산자를 안에서 구현할 수 없습니다. 이유는 360도를 넘어가는 값이 구현되어 오류를 발생할 수 있으므로 따로 _yAngle 에서 deltaTime과 100.0f를 곱해준뒤 넣어준 것입니다. 절대값을 사용하고 싶을때 사용하는 방법입니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour
{
float _yAngle = 0.0f;
void Update()
{
_yAngle += Time.deltaTime * 100.0f;
//절대 회전값
transform.eulerAngles = new Vector3(0.0f, _yAngle, 0.0f);
}
}
2. +- delta
- 특정 축을 기준으로 얼마만큼 회전하고 싶다 라고 하면 Rotate를 사용하면 됩니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotation : MonoBehaviour
{
void Update()
{
// +- delta
transform.Rotate(new Vector3(0.0f, Time.deltaTime * 100.0f, 0.0f));
}
}
Quaternion.LookRotation을 이용하여 캐릭터가 진행방향을 바라보도록 설정
-Quaternion은 간단하게 float을 4가지를 가지고 있습니다.(x, y, z, w)
-wasd를 클릭해보면 진행방향을 바라보게 됩니다. 하지만 문제점은 부드럽게 회전되지 않습니다.
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.rotation = Quaternion.LookRotation(Vector3.forward);
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.LookRotation(Vector3.back);
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.LookRotation(Vector3.left);
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.LookRotation(Vector3.right);
}
}
}
Quternion.Slerp() 을 이용하여 부드러운 회전 구현
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
float _speed = 10.0f;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
}
}
}
회전 방향으로 이동까지 구현
- Position 게시글에서는 translate를 이용하여 이동을 구현했는데 여기서 translate로 구현하면 문제점이 모델 기준 좌표로 움직이기 때문에 지그재그로 움직이는 현상이 발생하게 됩니다. 그래서 월드 좌표를 사용하여 구현하였습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
float _speed = 10.0f;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
transform.position += Vector3.forward * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
transform.position += Vector3.back * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
transform.position += Vector3.left * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
transform.position += Vector3.right * Time.deltaTime * _speed;
}
}
}
---------------------------------------------------------------------------------------------------------------------------
참고 : [인프런] Rookiss님의 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part3: 유니티 엔진
'C#, 유니티 > MMO RPG 만들기 기초(클라이언트)' 카테고리의 다른 글
| [MMO RPG] Trigger (0) | 2022.01.21 |
|---|---|
| [MMO RPG] Collision (0) | 2022.01.21 |
| [MMO RPG] Collider (0) | 2022.01.21 |
| [MMO RPG] Position (0) | 2022.01.19 |
| [MMO RPG] Player 간단한 움직임 구현 (0) | 2022.01.17 |