Best way to render bullet tracers?

I’m trying to make a raycast gun, and I’ve tried the DrawLine function to render tracers, but it only lasts for one frame, and is extremely thin. Is there a better way to render bullet tracers?

Hi @The_J! I have created something to get a line, with using a material on a render component. This need to be on an entity that has a parent entity. Below some steps you need to do.

Editor:

  1. Create a parent entity with a child entity
  2. Add a render component to the child entity

Script:

  1. Set the position of parent entity to raycast start point
  2. Use lookAt function to rotate the entity to raycast end point
  3. Determine the distance between the two points
  4. Set the local scale of the entity and use the distance on the right axis
1 Like

I’ve made some code for this, but I can’t exactly figure out why it’s not working (box is not rendering in the scene)
(result is a raycastresult)

        tracerBeam.addComponent('render', {type: 'box',});
        tracerBeam.setPosition(this.gunEntity.getPosition());
        tracerBeam.lookAt(result.point);
        tracerBeam.setLocalScale(this.gunEntity.getPosition().distance(result.point), 1, 1);
        this.app.root.addChild(tracerBeam);

As far as I can see line 1 should be on a child entity. Also I’m not sure if the render component has already a material on it. Maybe you need to add this manually.

Hello, i’m trying to do the same thing. This is my code:

Gun.prototype.shoot = function (screenPosition) {
    this.isReloading = false;
    if (this.currentAmmo == 0) {
        console.log("out of ammo");
        return false;
    }

    var centerScreenX = this.app.graphicsDevice.width / 2;
    var centerScreenY = this.app.graphicsDevice.height / 2;


    var from = this.cameraEntity.getPosition();
    var to = this.cameraEntity.camera.screenToWorld(centerScreenX, centerScreenY, this.cameraEntity.camera.farClip);
    var result = this.app.systems.rigidbody.raycastFirst(from, to);

    var bulletTrail = this.bulletTrail.clone();
    this.app.root.addChild(bulletTrail);

    this.bulletTrail.setPosition(this.from);
    this.bulletTrail.lookAt(result.point);
    this.bulletTrail.setLocalScale(this.entity.getPosition().distance(result.point), 1, 1);

I get this error when i left click “Uncaught TypeError: Cannot read properties of null (reading ‘point’)”. What could be the reason?

The reason could be that you don’t get a result with line 14 of your code.

raycast works right now. how should i define the result variable?

If the raycast works you can try it with a statement.

if (result) {
    // line 19 to 21
}

i stopped getting errors but nothing comes out of the gun. Besides i get too many warnings. I guess that’s bcs Play Canvas got an update a little while ago

I don’t think the warnings are coming from this method to be honest. Can you share a link of your project so I can see what you try to do?

https://playcanvas.com/editor/scene/1528185

I’m trying to add a bullet trail effect when i fire the gun, just like the OP tried to do

no they’re not. As i said i think that’s bcs of the recent PC update. It says stuff like refraction map etc. Nothing to do with my scripts, i didn’t get them before

10 posts were split to a new topic: Error (reading ‘getGuid’)

I think this.from on line 19 of the script above should be from? Also do you enable the entity somewhere to be able to see the result?

@Ronin I had a look to the project several lines above.

It’s throwing an error in this code.

 if (result) {

        console.log("You Hit: " + result.entity.name);
        this.handleImpact(result.entity, result.point, result.normal);

    } else {

        console.log("Nothing Hit");
    }

this.handleImpact is not declared anywhere in this software module.

“Also do you enable the entity somewhere to be able to see the result?” No, i didn’t. Should i enable it in the result if statement?

Yes. I’m also not sure about line 16 and 17 of the script above. I think you should prepare this in the editor already, because otherwise you get a lot of entities with bullet tracers.

I commented out those lines for now. Still nothing comes out of the gun. There’s BulletTrail parent entity and bullettrail child render entity(cone). I disabled both in the scene. I’m trying to enable them like this:

Is there something wrong with this?

Currently you only enable the parent by script while the child is also disabled, but you can enable the child in the editor by default.

Apart from that, the entity is currently on the root so you can’t find it with line 85. I suggest to use the line below. I assume you are adding the entity to an attribute?

this.bulletTrail.enabled = true;

yes but the parent entity