Best way to render bullet tracers?

That’s right. :ok_hand:

Ok, i think i got this. What i need to do now are:
1- Set the rotation of the projectile right :heavy_check_mark:
2-Make it get destroyed after a certain time i fire the gun
3-Figure out to change the localScale values in the code, bcs this doesn’t look good
4- Make it get created in every frame like Yaustar said

First of all because you use code for something you don’t want to achieve. You probably also use the distance on the wrong axis causing the result is not as intended. If everything is correct you will see a ‘line’ between raycast start and raycast end.

Why? Do you understand the raycast is only one frame? If you move the bullet as fast as the raycast you can’t see the bullet anyway?

"First of all because you use code for something you don’t want to achieve.

I thought i could modify the code as i want it to be.

“You probably also use the distance on the wrong axis causing the result is not as intended. If everything is correct you will see a ‘line’ between raycast start and raycast end.”

No, that works as it’s supposed to, you can check the project. I’m just not sure that i want it to be an infinite line also i used a cone object as per Yaustar’s suggestion but the shape of it when it’s streched doesn’t seem right to me but this isn’t so important right now maybe i change the object to a box i don’t know

“Why? Do you understand the raycast is only one frame? If you move the bullet as fast as the raycast you can’t see the bullet anyway?”

As per this suggestion: “When the player fires, it creates a bullet and every frame, it moves at a set speed in the direction the player is shooting in. It would also raycast from the position it was in last frame to it’s new position this frame.”

It sounds to be the right way to me

I changed a couple of things and I get almost something I expect to see.

I notice still some strange behaviour and I found out that’s because you use the orginal entity instead of the clonend entity. So you need to change that too.

image

I checked my own project and I see I determine the distance in another way, so maybe you can try that yourself too.

Alright, I think I get it. In that case it’s another goal and offtopic here.

I applied your changes to my script. The line i added for the rotation was unnecessary so i deleted it.So what you did in this line is “this.bulletTrail.setLocalScale(0.1, 0.1, this.entity.getPosition().distance(to));” you set the x,y axis of the cone to 0.1 and z axis to where the raycasting is going, starting from the entity this script is attached to. Cone always streches towards the z axis according to the player’s position whenever i fire. I have another problem with the scale here, i noticed it later. The line doesn’t start from the starting point point of the raycasting, it streches inifinitely to both directions(z and -z)

I couldn’t figure out what causes this problem.

“I notice still some strange behaviour and I found out that’s because you use the orginal entity instead of the clonend entity.”

I don’t get this. When i declared "var bulletTrail = this.bulletTrail.clone(); " didn’t bulletTrail variable became the clone within the scope of this function. Do i have to add “clone()” to it everytime i use it?

“I checked my own project and I see I determine the distance in another way, so maybe you can try that yourself too.”

I can’t make out much from only this function, i need to see the whole script. Can you share me the link of the project? Also there are variables like enemyViewline, enemyTarget. If you adjusted that code according to enemy’s distance to the player’s, that’s not how it’s supposed to be. Bullet trail needs to be visible anytime you fire, not just when you hit something

“Alright, I think I get it. In that case it’s another goal and offtopic here.”

I beg to differ. I think you’re assuming that bcs the creator of this post used the word"drawLine" once in their comment. Their aim was to create a bullet tracer effect when they fire the gun and drawLine is one method they could come up with upon their research. So i think drawLine is a means here not ends. The purpose of the OP was not to just create a line on the scene when they fire. Therefore i’m sorry but i don’t agree with your interpretation here. Also there are other reasons i refer to this topic for support but i don’t want to prolong this message further

If I have some more time I try to figure out why this happens.

With the code, bulletTrail becomes a clone of this.bulletTrail. Because we want to use the clone, we need to use bulletTrail and not this.bulletTrail.

If I translate my code to your project, the enemy will be your raycast end and the player will be your raycast start.

Even if I misunderstand the goal of the user, my answer gives an alternative for the problems the users described: “I’ve tried the DrawLine function to render tracers, but it only lasts for one frame, and is extremely thin”. The start and end point of the line can basically be everything, so if you want to create only a short line to create a “bullet tracer” that’s possible.

So what are you trying to achieve at the moment?

1 Like

If you want to quote some text, you can select the text and choose ‘Quote’.

image

I changed it but i haven’t observed any changes so far

If i do that will the bullet traces visible when i just shoot the air or will they be visible only when i hit the enemy?

I don’t object to your alternative, i’m objecting to your statement that my problem is irrelevant with the subject of this topic. I’ll try to realize the Yaustar’s suggestion to make the bulletTrace move from the source to destination in a certain time, not instantly. If i can’t manage to do it, i’ll try your method. But in most of the Unity tutorials i watched, they do it that way. You can watch this video

I’m trying to make the bullet traces visible as they’re fired from the gun like in most FPS games as i have stated before. As to why i refer to this topic; i created another topic for this issue:

I was given an example project that shows how to render a line on runtime. I coulnd’t figure out how to implement that script to my project, it was too complicated for me. After doing some more research on google, i found this topic and this was the closest thing i could find that could help me

Is the effect I use in my game the same as you try to achieve? (Hold player and drag to move).

yes, they’d work for me

Below are the steps on how to achieve this.

  1. Create a bullet (with tracer) in the editor (using render components).
  2. Make the bullet a part of the gun and give it the right position and rotation.
  3. Create a script for the bullet to move the bullet forward when enabled and to destroy it when need.
  4. Disable the bullet in the editor.
  5. Clone the bullet when the player shoot.
  6. Copy the position and rotation of the original bullet.
  7. Enable the cloned bullet.
2 Likes

I already have the code to move the bullet. Bullet doesn’t need to be under the gun, i instantiate it from an entity under each gun. I’ll do as you recommend. I’m gonna use raycasting to deal damage, kinematic rigidbody or forces to move the bullet. How do you deal damage to the enemies in your project?

@Ronin What I do is make an event inside the script for the enemy. When the raycast hits the enemy you will get a result pointer to the entity you hit. You can then use .fire to send an event to that enemy in your scene. There is an example inside this work in progress.

https://playcanvas.com/project/1002406/overview/halloween-jaunt--remix2

1 Like

That’s correct, but I did that because it makes it easier to get the right position and rotation.

Not my recommondation to be honest.

I use the bullet as a trigger.

var Bullet = pc.createScript('bullet');

Bullet.attributes.add("parent", {type: "entity", title: "Parent Entity"});

// initialize code called once per entity
Bullet.prototype.initialize = function() {
    this.entity.collision.on('triggerenter', this.onTriggerEnter, this);
};

// update code called every frame
Bullet.prototype.update = function(dt) {
    if (this.entity.enabled) {
        this.entity.translateLocal(0, 25 * dt, 0);
    }
};

Bullet.prototype.onTriggerEnter = function(result) {
    if (result.name == 'Enemy') {
        result.script.enemy.setHealth();
        
        if (this.parent.name == 'Player') {
            if (result.script.enemy.enemyState === 'Dead') {
                this.parent.script.player.setScore(1);
            }
        }
    }
    
    else if (result.name == 'Player') {
        result.script.player.setHealth();
    }
    
    this.entity.destroy();
};

Be aware all your static rigidbodies should be kinematic, otherwise the trigger doesn’t work.

I just notice I forget the automatic destroy when there is no collision, so I need to add that.

1 Like

You tell me it was my recommendation, so I’m just letting you know it wasn’t. You ask me how I do it, so I’m just showing you how I did it. It’s your project, so you can do whatever you prefer.

2 Likes

@Ronin I think you may have taken the suggestion above incorrectly.

I am not understanding how you interpreted @Albertos advice but maybe here is some more information. If the target is moving the collision and rigid body must follow along with it. Static rigid bodies will stay in place. Not sure why this would be a complete rewrite of things as it is a simple selection in the editor for your item that will be moving. I think I remember we discussed this same issue in this post.

1 Like

I mentioned the kinematic rigidbody because a moving trigger is not activated by a static rigidbody. Just in case someone would do the same.

1 Like

There’s a lot of information i need to process right now, i’m just concerned about losing my focus. I’m gonna get my head my together and look into this tomorrow. I’ll post here when i get some results. Until then, any comment i would make will just complicate things. Again, i thank everybody who tried to help me on this topic

Ok i did it. The damage is being dealt with the raycasting system while i fire the bullet with forces. When i inrease the speed of the bullet, you can’t tell the time difference between raycast hit and bullet’s impact. This is the code piece which launches the bullet entity

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

    var player = this.entity;
    var target = this.entity.findByName('target');
   
    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);

I don’t know why i was so obsessed with doing this with raycasting, there’s no visible difference and this is much easier. I apologize for any trouble and annoyance i dealt to anybody who tried to help me. I have utmost gratitude to all of you. I also hope that other users who are struggling about the issue would benefit from this topic. Infinite thanks :pray:

1 Like