我如何才能让岩石落在玩家身上,与岩石下方的触发框相撞

  • 本文关键字:石下 石落 玩家 c# xna
  • 更新时间 :
  • 英文 :

protected override void Update(GameTime gameTime)
{
int dt = gameTime.ElapsedGameTime.Milliseconds;
base.Update(gameTime);
player.storedPos = player.position;
Vector3 storedAcc = acceleration;
acceleration = new Vector3(0, 0, 0);
if (Keyboard.GetState().IsKeyDown(Keys.Left)) player.rotation.Y += 0.1f;
if (Keyboard.GetState().IsKeyDown(Keys.Right)) player.rotation.Y -= 0.1f;
player.velocity *= 0.9f; // friction
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
acceleration.X = (float)Math.Sin(player.rotation.Y) * 0.001f;
acceleration.Z = (float)Math.Cos(player.rotation.Y) * 0.001f;
}
// camera follow
gamecam.position = new Vector3(50, 50, 50) + player.position;
gamecam.target = player.position;
MovePlayer(dt);
foreach (basicCuboid WallSegment in walls)
{
if (player.hitBox.Intersects(WallSegment.collisionbox))
{
ElasticCollision(WallSegment);
}
}
if (player.hitBox.Intersects(door.collisionbox))
{
ElasticCollision(door);
}
if (player.hitBox.Intersects(TriggerBoxRockFall) && !rockFalling)
{
rockFalling = true;
rock.velocity = new Vector3(0, 0.2f, 0);
}
if (rockFalling)
{
Vector3 gravity = new Vector3(0, -0.01f, 0);
}

所以这是我到目前为止的代码,我不确定如何让岩石真正下落。我遗漏了什么,或者我打错了什么?我需要岩石落在玩家身上,与岩石下面的扳机盒相撞。

从提供的代码中还不清楚,但你只想让player.hitBox.Intersects(TriggerBoxRockFall(的代码在你低于它时触发。

更改代码:

if (player.hitBox.Intersects(TriggerBoxRockFall) && !rockFalling)
{
rockFalling = true;
rock.velocity = new Vector3(0, 0.2f, 0);
}

至:

if (player.Velocity.Y < 0 && player.hitBox.Intersects(TriggerBoxRockFall) && !rockFalling)
{
rockFalling = true;
rock.velocity = new Vector3(0, 0.2f, 0);
}

顺便说一句,尽可能多地将代码移出Game1。岩石的重力应该在岩石中完成,而不是在游戏1中。

例如:

if (player.Velocity.Y < 0 && player.hitBox.Intersects(TriggerBoxRockFall) && !rockFalling)
{
rockFalling = true;
rock.velocity = new Vector3(0, 0.2f, 0);
rock.GravityApplies = true;
}

最新更新