[SOLVED] Delay for the shot

Hey. Tell me, please, how to make a delay for the shot, so that the boxes do not immediately fly off?

Link to the project: https://playcanvas.com/editor/scene/678268

Are you using raycasts? I would create a bullet script for an entity that moves in a straight line at a set speed and perform a raycast between the current and previous frame position.

Yes, I use raycast, which is aimed forward from the weapon. Can you give me an example code, please?

Do you still need help with this? @MasalovAndreey? If so, I can look at making a fork of your project.

1 Like

Thanks for the offer to help, but I already figured out. I made a counter using the deltatime variable that simulates a delay shot. Then I added the entity of the bullet and turned it off. When you click the left mouse button, the reload counter starts and a new bullet entity is generated, which moves forward, using the .translate() method. As well the entity of the bullet makes RayCast in the opposite direction at a certain distance. When the entity of a bullet collides with a dynamic object, the .applyImpulse() method for the detected object is executed. Thank you again :slightly_smiling_face:

1 Like

Did you actually want a delayed shot or a simulation of a bullet travelling a distance? The first option doesn’t take into account of how far the target is. (Closer objects get hit sooner compared to objects far away)

Edit: Nevermind, I see what you have done now :slight_smile:

My project does not use a bullet but a laser that does not change the trajectory during the flight. To simulate ballistics, I have apply a mathematical formula at each frame that will change the trajectory of a bullet? Just wondering how to do it :grinning:

I be doing the same and doing a raycast between the new position and previous frame position.

Actually, you could just apply ‘gravity’ to the bullet every frame that it is going forward (world down * magic number * deltaTime). That would do enough to look like a bullet falling over a distance.

If I be used gravity from the physics engine, and adjust the mass parameter, will it work?

If the bullet has a rigid body and collision, then yes but I don’t recommend that bullets should use the physics engine due to their size and speed.

For large things like geneades, perhaps.

Thanks for the detailed explanation. Now in my project, shooting works well, but sometimes the bullet flies through the object. How can this problem be solved?

Assuming it isn’t using a rigidbody with collision:

Have the bullet keep track of it’s position in the world and do a raycast between it’s current and last frame position.

I tried this method, but it did not work. Could you help me? :slightly_smiling_face:

Try this version: https://playcanvas.com/project/596138/overview

A coupe of issues that I fixed:

  • The last position was updated after the entity had moved which meant that both the last position and the current position was the same.
  • The position of the entity wasn’t copied to last position, it was just storing a reference to the position vector.
1 Like

Great job. Now I understand my mistakes and how to do it right. :grinning:
Better not to use setTimeout(), but to use the deltaTime variable for all counters?

In your case, it looked like you were setting a setTimeout every frame the bullet was ‘hitting’ anything and because the code destroys the entity on ‘hit’, you can be left with dangling setTimeout calls which leads to exception errors.

The way I’ve done it means that there’s only one place that the entity is destroyed for the bullet.

Is my project correctly implemented the algorithm for calculating the target point for the bullet or can it be simplified?

From playing it, it looks a bit off. It’s almost as though it doing a raycast from the camera and not the players weapon. I haven’t looked at the code to be sure.

I simplified the script for the bullet, and I have new questions. :grinning:

  1. Can I somehow make a check on another entity, that Ray Cast from this script was successfully applied to it, and after that play, for example, a sound?

  2. If I have a trigger in my scene, but it doesn’t have a Rigid Body component, can I do the same check, that Ray Cast was applied, but ignore this so, that the .applyImpulse() method doesn’t throw an error?

  3. When I start the game, the bullet entity is disabled. When I take a shot, it’s copied. Euler angles always have the same value, regardless of where the weapon is directed. I have to set the Euler angles for each new bullet entity based on the Euler angles of the point that is at the end of the weapon. Can this be avoided?

I want to make the all scripts standalone so that they fit the design pattern ECS. :slightly_smiling_face: