How do I add gun motion to bullet

0 favourites
  • 6 posts
From the Asset Store
Complete the collection by purchasing Motion Universe Pt. 2
  • Sorry if this has been asked before. I can barely think of how to word it, so please bear with me.

    I have an object that is controlled using physics. Said object can fire a sprite with the bullet behaviour.

    When the object is stationary, and fires facing right, the bullet will move as expected. However, when the object is falling and it fires, I want the bullet to retain the vertical motion/speed, and then continue to move to the right.

    This is most noticeable when the object is facing down and it fires. The bullet can sometimes appear to be moving up, as it's moving slower than the "gun".

    As the object is physics based, I can't figure out how to apply the vector to the bullet behaviour. If I was using the physics behaviour as the bullet, I'd simply set the vector to the same and then apply an impulse in the direction I want it... but I don't want to use the physics behaviour for this.

    Hope I've made sense. If not, please let me know and I'll try again.

  • Here you go - here's an example capx using trigonometry.

    The velocity values from the physics and bullet behaviors match up well. I've commented it a bit - hope it helps!

  • Colludium Thanks! I'll have a look at this now! I think I was being thrown by velocity being based on per second rather than per tick.

  • Sometimes I find it's best not to think about these things too much, then I get caught out by not thinking things through....

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Colludium Very strange. When I apply this to my project, the bullet appears for 1 frame, and then vanishes. When I go through the debug menu and take a look at what the bullet is doing, it's X/Y vector and X Y coordinates are replaced with NaN (not a number).

    In addition, I note that the angle of motion is applied to the Y vector only. Would the same action work if the gun was moving only on the X axis aiming up as well, or would I need a new bit of code for that?

  • I think I may have cracked it... after realising that it was based on seconds rather than frames, and C2 defaults to 60fps... I set it so that each time a bullet is spawned, it stores the gun's x and y vector divided by 60. It then adds this data to its position every tick. Then when a speed is applied to it, the motion appears to work as expected.

    Looks a bit odd at times, but it looks pretty accurate.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)