How do I add gun motion to bullet

Get help using Construct 2

Post » Sat Jul 11, 2015 1:00 pm

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.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
84
S
15
G
7
Posts: 991
Reputation: 11,231

Post » Sat Jul 11, 2015 1:32 pm

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! :)
A big fan of JavaScript.
B
76
S
20
G
76
Posts: 2,285
Reputation: 47,554

Post » Sat Jul 11, 2015 1:40 pm

@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.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
84
S
15
G
7
Posts: 991
Reputation: 11,231

Post » Sat Jul 11, 2015 1:48 pm

Sometimes I find it's best not to think about these things too much, then I get caught out by not thinking things through....
A big fan of JavaScript.
B
76
S
20
G
76
Posts: 2,285
Reputation: 47,554

Post » Sat Jul 11, 2015 2:23 pm

@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?
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
84
S
15
G
7
Posts: 991
Reputation: 11,231

Post » Sat Jul 11, 2015 2:34 pm

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.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
84
S
15
G
7
Posts: 991
Reputation: 11,231


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests