[SOLVED] Delay for the shot


#21
  1. Not sure what you mean by this? If you are talking about telling the entity that it has been hit so it can do something in response, you can fire an event on the hit entity that scripts on that entity is listening for.

  2. Check if the hit entity has a rigidbody before applying impulse. (if (hitEntity.rigidbody != null) or something similar.

  3. Not really. You could put the bullet as a child of the weapon and place it at end of the barrel. When you clone, it should place it in the correct position to start from? This may cause problems if you reparent the bullet though. I would just stick to cloning it and positioning the clone in the correct place.


#22
  1. That is, I can configure the connection between the two scripts by this instruction?
  2. Here I understand.
  3. That is, I should always set Euler angles for the bullet entity, that I should be getting from another entity?

Sorry for a lot of questions. :sweat_smile:

Edit: I figured out with point 3. :grinning:


#23
  1. Yes. Entities have and components all can fire and listen to events which is incredibly useful

#24

You wouldn’t happen to know, there is rules for writing scripts based on architectural pattern Entity Component System?


#25

PlayCanvas script systems is more of a EC rather than ECS.

Quite frankly, I use the SOLID principles where possible.

I made this a while back (not sure if it still works with latest engine changes) which encompasses my approach in keeping code modular.

https://playcanvas.com/project/560025/overview/isometric-base-builder


#26

I, as a beginner, it is very interesting. :slightly_smiling_face:

I still have one more shooting question. How to set correctly the crosshair on the screen? I use the .worldToScreen method to set the crosshair, but it is installed incorrectly. I have to adjust it by pixels, which is not correct.


#27

As the crosshairs are on a 2D UI screen, you actually want world to UI space rather than world to screen (screen being your computer screen and working in pixels).

Unfortunately, there isn’t a built in function in PlayCanvas to do that but a few forum users have managed to work out how to do this: [SOLVED] Include devicePixelRatio for screenspace to worldspace conversion


#28

Thanks. I found this formula and applied it in my project. The crosshair should be at the bottom left of the screen.

var screenPos = new pc.Vec3();
var screenEntity = this.entity.element.screen;
var scale = screenEntity.screen.scale;
var device = this.app.graphicsDevice;

this.cameraCrosshairEntity.camera.worldToScreen(worldPoint, screenPos);
this.entity.setLocalPosition((screenPos.x / scale) - (this.entity.element.width / 2), ((device.height - screenPos.y) / scale) - (this.entity.element.height / 2), 0);

#30

@yaustar, could you help me? I found a problem with shooting, that if you shoot at boxes from above while jumping, the bullet almost always flies through. Can I somehow solve this problem? :slightly_smiling_face:


#31

The solution to the problem is in this topic.