简体   繁体   English

如何在Phaser 3中使子弹沿光标方向移动?

[英]How to make bullet move in direction of cursor in Phaser 3?

I am creating a 2D game and trying to make bullet move towards the cursor.我正在创建一个 2D 游戏并试图让子弹向光标移动。 I'm not sure how to do this is what I cuurently have我不知道该怎么做,这是我目前所拥有的

let xDist = this.game.input.mousePointer.x - this.x;
let yDist = this.game.input.mousePointer.y - this.y;
let angle = Math.atan(yDist/xDist);

this.projectile_sprite.setVelocityX(yDist);
this.projectile_sprite.setVelocityY(xDist);

The projectile moves faster when its further away which is not what I desire.射弹在距离较远时移动得更快,这不是我想要的。 How do I fix this?我该如何解决?

In Phaser, velocity is basically the speed at which your object travels towards an unspecified point.在 Phaser 中,速度基本上是您的对象向未指定点移动的速度。 You are setting the velocity of your projectile to the distance between the object and the cursor, so when the cursor is further from the object it will have a higher velocity and thus, move faster.您将射弹的速度设置为对象和光标之间的距离,因此当光标离对象越远时,它的速度就会越高,从而移动得更快。

Instead, you want to find the position of the cursor and set your projectile object moving towards that point.相反,您想要找到光标的位置并将弹丸对象设置为朝着该点移动。 Since you're using setVelocityX() I'm assuming you are using a built-in physics library, so it should be as simple as this:由于您使用的是setVelocityX()我假设您使用的是内置物理库,所以它应该像这样简单:

this.physics.moveTo(this.projectile_sprite, this.game.input.mousePointer.x,
  this.game.input.mousePointer.y);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM