Start 1: 2025/02/18 16:55 ~ 2025/02/18 18:29
→ B15 00:00 ~ B15 18:53
Start 2: 2025/02/18 19:07 ~ 2025/02/18 20:41
→ B15 18:53 ~ B15 38:35
→ Complete
⇒ 2D Object 물리 적용 및 Animation 진행
PlayerMove.cs
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
// 2D 중력
Rigidbody2D rigid;
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
// 수평값 입력 받기
float h = Input.GetAxisRaw("Horizontal");
// Object 이동
rigid.AddForce(Vector2.right * h, ForceMode2D.Impulse);
}
}
PlayerMove.cs
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
// 2D 중력
Rigidbody2D rigid;
// 최대 속도
public float maxSpeed;
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
// 수평값 입력 받기
float h = Input.GetAxisRaw("Horizontal");
// Object 이동
rigid.AddForce(Vector2.right * h, ForceMode2D.Impulse);
// 속도 제어(오른쪽 이동)
if (rigid.linearVelocity.x > maxSpeed)
rigid.linearVelocity = new Vector2(maxSpeed, rigid.linearVelocity.y);
// 속도 제어(왼쪽 이동)
else if (rigid.linearVelocity.x < maxSpeed * (-1))
rigid.linearVelocity = new Vector2(maxSpeed * (-1), rigid.linearVelocity.y);
}
}
rigid.linearVelocity.x
: 최소, 최대 속도가 maxSpeed
로 제한rigid.linearVelocity.y
: 그대로 값을 유지public으로 선언하여 Inspector 창에 생성됨