私はXNAの宇宙船の試合をして、私は回して銃を発射しようとしています。私は船にウェイポイントを与えることができるようにそれを得ることを試みる問題に遭遇した。何らかの理由で私は船を動かすことができます、または私はそれを回すことができますが、私はそれを一度に行うことはできません。それは本当に奇妙です。C#XNAムーブメント - 両方を回すことも移動することもできません。
基本的には、回転コードをコメントアウトすると、まっすぐ飛ぶでしょう。さもなければ回転しますが、動きません。
if (GoToWayPoint == true)
{
//CurrentWayPoint.Normalize();
Pos.Normalize();
float angle = (float)Math.Atan2(CurrentWayPoint.X - Pos.X, CurrentWayPoint.Y - Pos.Y);
//Rotate if
if (Rotation > angle && Rotation - TurnSpeed >= angle)
{
Rotation -= TurnSpeed;
}
else if (Rotation < angle && Rotation + TurnSpeed <= angle)
{
Rotation += TurnSpeed;
}
else
{
Rotation = angle;
}
//Move forward if it's facing the right direction
if (Rotation == angle)
{
Velocity.X += (float)Math.Cos(Rotation) * TangentialVelocity * Speed;
Velocity.Y += (float)Math.Sin(Rotation) * TangentialVelocity * Speed;
}
if (Pos == CurrentWayPoint || Vector2.Distance(Pos,CurrentWayPoint) < 10)
{
Pos = CurrentWayPoint;
GoToWayPoint = false;
}
}
//Movement, player controlled
if (PlayerControlled == true)
{
KeyboardState NewKey = Keyboard.GetState();
//Move Up
if (NewKey.IsKeyDown(Keys.Up) || NewKey.IsKeyDown(Keys.W))
{
Velocity.X = (float)Math.Cos(Rotation) * TangentialVelocity * Speed;
Velocity.Y = (float)Math.Sin(Rotation) * TangentialVelocity * Speed;
}
//Move Down
if (NewKey.IsKeyDown(Keys.Down) || NewKey.IsKeyDown(Keys.S))
{
Velocity.X = (float)Math.Cos(Rotation + Math.PI) * TangentialVelocity;
Velocity.Y = (float)Math.Sin(Rotation + Math.PI) * TangentialVelocity;
}
//Move Right
if (NewKey.IsKeyDown(Keys.Right) || NewKey.IsKeyDown(Keys.D))
{
Rotation += TurnSpeed;
}
//Move Left
if (NewKey.IsKeyDown(Keys.Left) || NewKey.IsKeyDown(Keys.A))
{
Rotation -= TurnSpeed;
}
}
//Update Movement
Pos += Velocity;
//Friction
float i = Velocity.X;
float j = Velocity.Y;
Velocity.X = i -= Friction * i;
Velocity.Y = j -= Friction * j;
さらに、矢印キーを使用してGameObjectを制御できるコードがあります。そのコードは正常に動作し、同じタイプコードを使用して前進します。
'(GoToWayPoint == true)'を実行して条件付きの評価をする必要がないことを伝えたいだけです。 'GoToWayPoint'は単独でtrueかfalseに評価されます。 – paste