Best way to render bullet tracers?

@Ronin I agree with @yaustar and @Albertos the entire illusion of firing something is mainly done by the process of raycast and physical items being fired take up a lot of resources and time. I found another video which may capture your idea. I have not watched it to the end but it may show you some different ideas.

2 Likes

ah sry for the misunderstanding. “When the player fires, it creates a bullet and every frame” how do i do that? With something like this: this._time += dt;. They did it in the video with this i think:
if(LastShootTime + ShootDelay < Time.time)

he uses trail renderer component of Unity for the bullet trail and particle system for the muzzle flash and he also uses Time.time to create a bullet every frame as i understand

when i add anything in the if(result) statement it only activates when i hit something. Bullets should appear when i don’t hit an object too. I don’t think shooting bullets code need to be inside the result statement

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