统一:球表现出奇怪的行为
UNITY : Ball showing odd behaviour
我的游戏场景中有一个 Ball(sphere) 和一个 floor。 Ball.cs
附在球上,控制球在游戏中的运动(球只在垂直方向运动)。 Ball 和 floor 都有碰撞器,只要球接触到地板,游戏就应该结束。
OnCollisionEnter2D 来自 Ball.cs
脚本的方法。
private void OnCollisionEnter2D(Collision2D collision)
{
// Zero out the ball's velocity
rb2d.velocity = Vector2.zero;
// If the ball collides with something set it to dead...
isDead = true;
//...and tell the game control about it.
GameController.instance.PlayerDied();
}
更新函数
void Update () {
//Don't allow control if the bird has died.
if (isDead == false)
{
//Look for input to trigger a "flap".
if (Input.GetMouseButtonDown(0))
{
//...zero out the birds current y velocity before...
rb2d.velocity = Vector2.zero;
// new Vector2(rb2d.velocity.x, 0);
//..giving the bird some upward force.
rb2d.AddForce(new Vector2(0, upForce));
}
}
}
但实际情况是,每当球接触地面时,它就会开始在地面上滚动。它在 +X-axis
上移动了几个单位,然后回滚并最终停止。
position.X
理想情况下应该为 0(因为球只在 Y-axis
中移动,而且是在比赛期间)但是一旦球与地板碰撞它就开始移动。
我是 Unity 的新手,我不知道哪里出了问题。
为什么会这样?
编辑:
程序员的回答确实有效,但我仍然不明白水平速度来自哪里(有与球相关的水平速度分量)。我需要知道为什么球在水平方向移动。
我注意到您在发生碰撞时将 isDead
设置为 true。如果您不希望球再次移动,则在 Update
中将速度设置为 Vector2.zero;
,而不仅仅是 OnCollisionEnter2D
函数中的 。仅当 isDead
为真时才执行此操作。
void Update()
{
if (isDead)
{
rb2d.velocity = Vector2.zero;
}
}
另一种选择是在碰撞发生时冻结约束。如果您想让球再次开始滚动,请将其解冻。
private void OnCollisionEnter2D(Collision2D collision)
{
//Zero out the ball's velocity
rb2d.velocity = Vector2.zero;
//Freeze constraints
rb2d.constraints = RigidbodyConstraints2D.FreezeAll;
// If the ball collides with something set it to dead...
isDead = true;
//...and tell the game control about it.
GameController.instance.PlayerDied();
}
执行rb2d.constraints = RigidbodyConstraints2D.None;
之后解冻。
我的游戏场景中有一个 Ball(sphere) 和一个 floor。 Ball.cs
附在球上,控制球在游戏中的运动(球只在垂直方向运动)。 Ball 和 floor 都有碰撞器,只要球接触到地板,游戏就应该结束。
OnCollisionEnter2D 来自 Ball.cs
脚本的方法。
private void OnCollisionEnter2D(Collision2D collision)
{
// Zero out the ball's velocity
rb2d.velocity = Vector2.zero;
// If the ball collides with something set it to dead...
isDead = true;
//...and tell the game control about it.
GameController.instance.PlayerDied();
}
更新函数
void Update () {
//Don't allow control if the bird has died.
if (isDead == false)
{
//Look for input to trigger a "flap".
if (Input.GetMouseButtonDown(0))
{
//...zero out the birds current y velocity before...
rb2d.velocity = Vector2.zero;
// new Vector2(rb2d.velocity.x, 0);
//..giving the bird some upward force.
rb2d.AddForce(new Vector2(0, upForce));
}
}
}
但实际情况是,每当球接触地面时,它就会开始在地面上滚动。它在 +X-axis
上移动了几个单位,然后回滚并最终停止。
position.X
理想情况下应该为 0(因为球只在 Y-axis
中移动,而且是在比赛期间)但是一旦球与地板碰撞它就开始移动。
我是 Unity 的新手,我不知道哪里出了问题。
为什么会这样?
编辑: 程序员的回答确实有效,但我仍然不明白水平速度来自哪里(有与球相关的水平速度分量)。我需要知道为什么球在水平方向移动。
我注意到您在发生碰撞时将 isDead
设置为 true。如果您不希望球再次移动,则在 Update
中将速度设置为 Vector2.zero;
,而不仅仅是 OnCollisionEnter2D
函数中的 。仅当 isDead
为真时才执行此操作。
void Update()
{
if (isDead)
{
rb2d.velocity = Vector2.zero;
}
}
另一种选择是在碰撞发生时冻结约束。如果您想让球再次开始滚动,请将其解冻。
private void OnCollisionEnter2D(Collision2D collision)
{
//Zero out the ball's velocity
rb2d.velocity = Vector2.zero;
//Freeze constraints
rb2d.constraints = RigidbodyConstraints2D.FreezeAll;
// If the ball collides with something set it to dead...
isDead = true;
//...and tell the game control about it.
GameController.instance.PlayerDied();
}
执行rb2d.constraints = RigidbodyConstraints2D.None;
之后解冻。