【发布时间】:2021-08-08 17:17:33
【问题描述】:
所以我对统一非常非常非常陌生,我在这里学习如何制作基本平台游戏的教程,这样我就可以学习基本的刚体控制和类似的东西
除了这一点之外,所有这些都很好,每当我进入墙壁时,我都会卡在那里,直到我松开作为我的移动键的 A 或 D。
这是我的代码,我已经尝试实施修复,但没有奏效并且进一步破坏了它。它将被注释掉并用注释突出显示它在我的代码中的位置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movementScript : MonoBehaviour
{
public float movementSpeed = 50;
public Rigidbody2D rb;
public LayerMask groundLayer;
public float jumpForce = 20f;
public Transform feet;
float mx;
private void Update()
{
Debug.Log(transform.position);
// me trying to fix the error
// if(isGrounded() && !rb.IsTouchingLayers()){
// return;
// }
// end of me trying to fix the error
mx = Input.GetAxis("Horizontal");
if (Input.GetButtonDown("Jump") && isGrounded())
{
jump();
}
}
private void FixedUpdate()
{
Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
rb.velocity = movement;
}
void jump()
{
Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
rb.velocity = movement;
}
public bool isGrounded()
{
Collider2D groundCheck =
Physics2D.OverlapCircle(feet.position, 0.5f, groundLayer);
if (groundCheck)
{
return true;
}
return false;
}
}
如果您需要,这就是我的项目的外观。 image of project here
【问题讨论】:
-
您正在用您自己的运动矢量覆盖游戏引擎设置的力。您可能想要的是
AddForce,而不是明确设置它:docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html -
实际上,忽略这一点,我看到您已经采用了现有的 Y 速度 - 听起来您的接地检查是假设您已接地。你调试过地面检查吗?您使用的圆圈可能比您的角色更宽,因此在您接触墙壁时会检测到地面。
-
@Charleh 这应该不是问题,因为我的边界不在底层
-
尝试将
Time.fixedDeltaTime加到Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);因为现在每个报价都将mx乘以movementSpeed,可能会创造很大的价值。我会推荐Vector2 movement = new Vector2(mx * movementSpeed * Time.fixedDeltaTime, rb.velocity.y); -
@Charleh
AddForce()的原始答案有效,非常感谢您的帮助!