Bullets not moving

I used applyForce on my bullets but they don’t move. Why?

https://playcanvas.com/editor/code/539704?tabs=11418589&line=22&col=43&error=true

You are only applying force for a single frame which is the equivalent of just tapping it. For things like bullets, applying impulse for a single frame (which is the equivalent of giving it lots of energy) would be your best bet.

Also note that you are creating an entity EVERY frame with physics which means that you are allocating memory and creating a new physics object in the world simulation every frame.

If you only create the entity when you fire (ie, when the player presses X), that will be much better.

2 Likes

Tis not working… even though I did an impulse

My bad, I forgot kinematic rigidbodies can only be moved via teleport or by setting the velocity directly. They don’t react to impulses or forces as noted in the API docs: https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#type

I made the change here during testing and deliberately left some of the bugs that you had in the original scene here as well: https://playcanvas.com/editor/scene/596497

1 Like

Thanks, but there’s one problem: the bullets move in the same direction. Thank you profusely for the help, though; I finally feel like I’m making progress on the whole shooting bullets thing.

Yes, that was one of the bugs from your original code and I deliberately left it in for you to have a go at fixing. Velocity, force and impulse all are represented with direction in world space. Right now, you are giving all the bullets the same velocity.

I figured out half of it… but it’s super slow.
https://playcanvas.com/editor/code/539704?tabs=11418589

Oh wait, I got it. Thanks a TON! I have been spending forever trying to implement this mechanic and it’s finally working!
https://launch.playcanvas.com/592996?debug=true