Start 1: 2025/02/18 16:55 ~ 2025/02/18 18:29B15 00:00 ~ B15 18:53

Start 2: 2025/02/18 19:07 ~ 2025/02/18 20:41B15 18:53 ~ B15 38:35Complete

⇒ 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);
    }
}

실행 화면

Movie_001.mp4

속도 제어

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);
    }
}

image.png

public으로 선언하여 Inspector 창에 생성됨

실행 화면

02_Player_maxSpeed.mp4

마찰력 감소

image.png