Ln Go

개발 일기 1 PlayerMove 본문

Unity/Maple Story

개발 일기 1 PlayerMove

Ln Ro 2021. 6. 5. 00:47

Rigidbody 2D 에서 Linear Drag(sometimes called air resistance)를 설정해주지 않아 Player가 계속 움직였다. 

OnCollisionEnter2D()로 더블 점프를 막아주려 했지만 OnCollisionEnter()을 써버렸다. 

BoxCollider2D로 진행시 다른 BoxCollider과 만나면 버벅이거나 캐릭터가 멈춘다. 

CapsuleCollider2D로 바꿔주었다.

using UnityEngine;

public class PlayerMove : MonoBehaviour
{
    Rigidbody2D rigid;
    public float maxSpeed;
    public float jumpPower;

    bool isGround;

    void Awake()
    {
        rigid = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (isGround)
        {
            if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.LeftAlt))
            {
                rigid.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
                isGround = false;
            }
        }

    }

    void FixedUpdate()
    {
        float h = Input.GetAxisRaw("Horizontal");
        rigid.AddForce(Vector2.right * h, ForceMode2D.Impulse);

        if (rigid.velocity.x > maxSpeed)
            rigid.velocity = new Vector2(maxSpeed, rigid.velocity.y);
        else if (rigid.velocity.x < maxSpeed * (-1))
            rigid.velocity = new Vector2(maxSpeed * (-1), rigid.velocity.y);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "BackGround")
        {
            isGround = true;
        }
    }
}

'Unity > Maple Story' 카테고리의 다른 글

메이플스토리 유니티로 만들기  (0) 2021.06.04
Comments