【问题标题】:Problem with my ground tiles and player movement Unity 2D我的地砖和玩家移动 Unity 2D 出现问题
【发布时间】:2021-11-14 16:44:49
【问题描述】:

我的玩家在地砖上的移动有问题。这是我第一次试用 Unity 2D。我正在尝试构建一个简单的 2D 平台。突然,水平地面变为垂直并开始滚动 [

我认为问题出在 Tilemap Collider 2D 上,但我不太确定。以下是我目前用于玩家移动的脚本:

公共类 playerMovement2D : MonoBehaviour {

protected Rigidbody2D body;
protected Vector2 velocity;

public float jumpVelocity = 20.0f;

float horizontal;
float vertical;
bool isGrounded = false;
public Transform isGroundedChecker;
public float checkGroundRadius;
public LayerMask groundLayer;
public float runSpeed = 10.0f;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;



// Start is called before the first frame update
void Start()
{
    body = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
    Move();
    Jump();
    BetterJump();
    CheckIfGrounded();
   
}

private void Move()
{
    horizontal = Input.GetAxisRaw("Horizontal");
    float moveBy = horizontal * runSpeed;
    body.velocity = new Vector2(moveBy, body.velocity.y);

}

private void Jump()
{
    if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    {
        body.velocity = new Vector2(jumpVelocity, body.velocity.x);
    }
}

void CheckIfGrounded()
{
    Collider2D collider = Physics2D.OverlapCircle(isGroundedChecker.position, checkGroundRadius, groundLayer);
    if (collider != null)
    {
        isGrounded = true;
    }
    else
    {
        isGrounded = false;
    }
}

void BetterJump()
{
    if (body.velocity.y < 0)
    {
        body.velocity += Vector2.up * Physics2D.gravity * (fallMultiplier - 1) * Time.deltaTime;
    }
    else if (body.velocity.y > 0 && !Input.GetKey(KeyCode.Space))
    {
        body.velocity += Vector2.up * Physics2D.gravity * (lowJumpMultiplier - 1) * Time.deltaTime;
    }
}

}

【问题讨论】:

    标签: unity3d 2d


    【解决方案1】:

    您是否冻结了 Rigidbody 2D 组件中的旋转?它列在限制条件下。

    【讨论】:

    • 如果我检查旋转约束,玩家有时会停下来。请指教!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2017-02-20
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多