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.
Check if the hit entity has a rigidbody before applying impulse. (
if (hitEntity.rigidbody != null)or something similar.
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.
- That is, I can configure the connection between the two scripts by this instruction?
- Here I understand.
- 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.
Edit: I figured out with point 3.
- Yes. Entities have and components all can fire and listen to events which is incredibly useful
You wouldn’t happen to know, there is rules for writing scripts based on architectural pattern Entity Component System?
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.
I, as a beginner, it is very interesting.
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.
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
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);
@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?