Preventing the idle animation from effecting the bullet direction

Hello, as the title suggests i’m having a problem in my FPS project about the idle animation of arms model effecting the direction of where the bullets are going when i fire the gun. This creates a sort of unintentional bullet spread pattern but it’s not supposed to be like this. I want to add the bullet spread with the recoil system. The only solution i could come up with so far to prevent this was to pause the animation when i fire and resume it again when i stop firing. I managed to do that yesterday but when i test the game, i saw that the problem(unintentional bullet spread) still persists, especially when i fire the pistol consecutively. I’m not quite sure how i can deal with this problem. It would be greatly appreciated if anyone can help me with this issue. Here’s a link of my project for you to see more clearly the problem i 'm having:
https://playcanvas.com/editor/scene/1528185

Hi @Ronin!

Your bullet will not go in the direction of your crosshair, because you use the forward direction of a target entity that is a child of the gun entity. The target entity is moving and rotating with the movement of your animation, while the crosshair is fixed on the screen.

Pause the animation will only work, if you also move the crosshair with the movement of your animation. For that the crosshair need to be a child entity of the gun entity. I would play the animation again, after the bullet is fired and not when you release the mouse button, like you currently do.

Apart from that, be aware that findByName() always returns the first entity of the hierarchy. You currently have a target entity with the same name for every gun entity. I see with your setup this is not a problem, because you use the script on every gun entity.

Perhaps an easier solution is to use one target entity that is not a part of the gun entity and animation. Make sure that the forward direction of this entity is already pointing to the crosshair.

I hope this helps.

2 Likes

Another option is to give your current target entities a script to set the forward direction always towards the crosshair. I think you need to use a raycast and lookAt() for this.

Or maybe better for performance, in your existing script and only before shooting.

1 Like

I already use raycast in my gun script for shooting. We even had a lengthy discussion about this in another topic. Best way to render bullet tracers? - #67 by Ronin

I initially wanted to move the bullets with raycast but you said that it isn’t possible then i used force impulse to move the bullets bcs i couldn’t figure out a different way for it. Could you elaborate a little more of how can i undertake this or can you direct me to a source which i can look at? Bcs the last time i tried what you mention in your comment , i failed to do so

Another option is to give your current target entities a script to set the forward direction always towards the crosshair. I think you need to use a raycast and lookAt() for this.

That’s right, because a raycast is going from A to B in one frame. If you do the same with a visual bullet, you can’t see the bullet moving. A visual bullet need a couple of frames to move from A to B to be able to see it. You can apply a raycast on the visual bullet, but I’m not sure why you would do that in your case.

You can make the rigidbody of the bullet kinematic and move the bullet forward with a script on the bullet. This way the bullet can move straight forward without forces. This would be my preference.

I’ve tried this and I’ve seen that you can do it in your current script with the raycast you’re already using.
All you need to do is add the line below after you have defined target and to (so that will be line 126 of your script).

    target.lookAt(to);
1 Like

oh wow, it actually worked!! The bullets are going straight!!! So i don’t need this part anymore right?

    this.force = new pc.Vec3();
    this.force.copy(target.forward);
    this.force.scale(this.gunpower);
    console.log(target);

    var pos = target.getPosition();
    var direction = target.forward;
    // Add it a little further if the player is already going fast
    // pos.add(direction.scale(5 + gun.rigidbody.linearVelocity.length() * 0.01));

    bullet.setPosition(pos);
    var bulletRotation = this.entity.getRotation();
    bullet.setRotation(bulletRotation);
    bullet.rotateLocal(0, 0, 90);

    bullet.enabled = true; //Must enable after setting position!

    bullet.rigidbody.applyImpulse(this.force);

no, i need that part. Hmm so raycasting works in conjunction with the force impulse here? Interesting

1 Like

That’s right, because a raycast is going from A to B in one frame. If you do the same with a visual bullet, you can’t see the bullet moving. A visual bullet need a couple of frames to move from A to B to be able to see it. You can apply a raycast on the visual bullet, but I’m not sure why you would do that in your case.

You know what i achieved with this “target.lookAt(to);” was exactly what i wanted to achieve in that topic. I probably couldn’t ask the question in the right way bcs i thought that it is being done in a similar way with Unity but the method for doing this is actually quite different in Play Canvas

Btw, stopping the animation contributes to the bullet’s direction as well with this method? I hope the time i spent for that didn’t go in vain!

Edit: I tried it, i disabled the code piece which pauses the animation. It looks like it doesn’t effect where the bullets are going at all

1 Like