c# - 为什么多边形对撞机 2d 会限制玩家的移动?
问题描述
我有一个附有多边形对撞机 2d 的云。另外,我有一个附加了 2d 盒对撞机的玩家。当玩家降落在云上时,在移动过程中的某个时刻,有什么东西阻止了他。他正在制作动画,但他一动不动。
下面是我的对撞机的图像:
当我开始运行游戏时,他左右移动。所以我认为这不是代码问题。在某一时刻,他被卡在上面的位置,他不能向右移动,但他可以向左移动。我猜多边形对撞机正在阻止他自由移动。当我回去时,他正在走路,当到达上述位置时,他无法前进。
有什么解决方法吗?
下面是我的代码:
public class Player : MonoBehaviour
{
public float speed = 7f;
public float maxVelocity = 8f;
private Rigidbody2D rb;
private Animator anim;
void Awake()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
MovePlayerUsingKeyboard();
}
public void MovePlayerUsingKeyboard()
{
float forceX = 0f;
float velocity = Mathf.Abs(rb.velocity.x);
Debug.Log("Player Velocity : " + velocity);
float direction = Input.GetAxis("Horizontal");
if (direction < 0)
{
if (maxVelocity > velocity)
{
anim.SetBool("Walk", true);
forceX = -speed;
}
//Changing the direction the player faces
Vector3 temp = transform.localScale;
temp.x = -1.3f;
transform.localScale = temp;
}
else if (direction > 0)
{
if (maxVelocity > velocity)
{
anim.SetBool("Walk", true);
forceX = speed;
}
Vector3 temp = transform.localScale;
temp.x = 1.3f;
transform.localScale = temp;
}
else
{
anim.SetBool("Walk", false);
}
rb.AddForce(new Vector2(forceX, 0));
}
}
解决方案
我认为这是因为您角色的对撞机卡在了云多边形对撞机的颠簸部分。一个解决办法是把角色的对撞机改成半胶囊对撞机或者胶囊对撞机,让角色可以在一些粗糙的表面上平稳行走。
推荐阅读
- java - 如何将其出现的参数设置为 jdbcTemplate 查询函数?
- python - 更改不同类中变量的值,无需重写方法
- ruby-on-rails - 数组元素的 Ruby 索引
- unit-testing - 使用完整的 .NET 框架对 AspNetCore 进行单元测试
- sql - 获取具有最大值的行
- python - 如何生成圆环结?
- azure - 如何在 Web 应用程序中将端口 6006 重定向到端口 80?
- javascript - Vuejs 不刷新其他组件
- react-native - 为什么刷新列表以获取其项目时,iOS 上不显示刷新指示器?
- c# - Skybrud 社交 Instagram 停止在网站上工作