Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 太空射手_C#_Xna - Fatal编程技术网

C# 太空射手

C# 太空射手,c#,xna,C#,Xna,我有一个旋转运动和射击的太空射手,但当我射击时,子弹会稍微向左或向右移动,我该如何解决这个问题谢谢:) Bullets newBullet=新的项目符号(Content.Load(“PlayerBullet”); newBullet.velocity=新矢量2((浮点)数学Cos(旋转), (浮动)数学Sin(旋转))*5f+游戏性; newBullet.position=播放器位置+newBullet.velocity*5; 下面是一些代码,我认为这是导致问题的原因我希望子弹的初始位置是 n

我有一个旋转运动和射击的太空射手,但当我射击时,子弹会稍微向左或向右移动,我该如何解决这个问题谢谢:)

Bullets newBullet=新的项目符号(Content.Load(“PlayerBullet”);
newBullet.velocity=新矢量2((浮点)数学Cos(旋转),
(浮动)数学Sin(旋转))*5f+游戏性;
newBullet.position=播放器位置+newBullet.velocity*5;

下面是一些代码,我认为这是导致问题的原因

我希望子弹的初始位置是

newBullet.position = playerPosition + playerVelocity * 5;
而不是

newBullet.position = playerPosition + newBullet.velocity * 5;

我相信你的位置和速度计算是错误的

初始位置应仅为玩家的位置(例如,如果携带枪支,可能会加上偏移量)

我不知道你为什么要把速度加到子弹上。为什么玩家移动的速度有多快会很重要?我还假设
*5f
只是一个速度系数

newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f;

尝试一下,看看这是否解决了问题。

我打赌你在创建子弹后会更新玩家位置,基本上是生成子弹,然后向左或向右移动玩家对象


在使用它来定位子弹、相机等之前,确保你已经完成了玩家位置的更改。

称之为“硬模式功能”哈哈,但不是简单模式:为什么
游戏性
会影响子弹?玩家移动时子弹会更快吗?是的,子弹会更快:)你可能需要从
newBullet.position
中减去
playerVelocity
。初始位置可能并不取决于玩家的速度。如果不是这样,请发布一些屏幕截图。否则就不可能判断出什么地方出了问题。@fWalk,我也不希望在向位置添加速度时出现
*5
。嗯,似乎没有任何明显的区别?如果没有区别,问题就出在其他地方。很抱歉
newBullet.position = playerPosition;
newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f;