- Home /
How to force a trail renderer to re-render in the same frame?
Hello,
I'm working on a bullet trail effect using a trail renderer. What I would like to do is to render the trail renderer again, in the same frame (the game object is moved before it should render again), is there any way to achieve this? I discovered there was a method available Render(Material mat);
but I couldn't get this to work. In the same frame, after the trail should be rendered the game object is being destroyed. What I've currently done is that the game object destroys itself the next frame (using a variable) so the trail renderer is able to render one more time, but I would like to have a function to force it to render, because that works way better than my current method.
Btw, I'm using C# code, but if someone has a JavaScript/UnityScript code to achieve this, I'm able to convert it to C# myself.
EDIT/ENCHANTMENT: What a trail renderer does, is that it renders a trail from the location it was on the previous frame, to the location it is currently on. Because the bullet projectile is flying about 500 meters per second, the bullet is flying through things. What I've done is that it casts a ray every frame, to check if there's anything in front of the bullet, and if it is it removes the bullet the frame before impact (again, otherwise the bullet will fly through the objects, because of it's speed). This causes the bullet to destroy itself already 10 meters before the impact. Of course I would like to render the bullet trail up to the impact position. Right now, the bullet trail just stops at the position the bullet is being destroyed. I've tried to set the position of the bullet, but because the trail render has to render again in a new frame this doesn't work. The current solution I have works as follows: The frame before impact, it places the bullet at the point of impact, and it sets the variable destroyNextFrame = true
. So the next frame, after the trail is being rendered again (which causes the trail to reach the impact position) it destroys itself once the Update()
function is being called. Bellow is a little example (C# Code):
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
/** @var bool destroyNextFrame Set whether the bullet should be destroyed the next frame (so trail renderers could finish rendering) */
private bool destroyNextFrame = false;
/**
* Called once each frame
*/
public void Update() {
// Destroy the bullet if set in the previous frame (so trail renders could render up to the impact position)
if(destroyNextFrame) {
Destroy(gameObject);
return;
}
// Code to reset the bullet speed and to calculcate impact position using raycasts here.
RaycastHit nearestHit = new RaycastHit(); // This variable contains the closet hit
if (nearestHit != null) {
// Move the bullet to the impact position, and reset the velocity.
// Also set destroyNextFrame to true so the trail render could render up to the impact position.
transform.position = nearestHit.point;
rigidbody.velocity = new Vector3(0, 0, 0);
destroyNextFrame = true;
}
}
}
I've cut out all the unnecessary parts in the above code. As you can see, the bullet calculates the impact position, the frame before impact it moves the bullet to the impact position and resets the velocity. Then it also sets destroyNextFrame = true
so the trail render could render up up the impact position. Then in the next frame when the trail is rendered, it destroys itself. (Yes I have the script to calculate the impact position, I've just cut it out to make it easier to read).
Now, finally, what I would like to have. Is that the trail renderer renders up to the impact position in a single frame. I was thinking of the following solution: The bullet location is set the impact position. The trail render renders again, so it reaches the impact position, then the bullet is destroyed, all in the same frame. I've tried to use the Render(Material mat);
inside the trail render component, but I couldn't get it to work (somehow, it crashed Unity).
I hope I explained my problem clear, if not, feel free to question.
Btw, I'm using a BulletManager script which puts the bullet in idle instead of destroying them, so they could be re-used for preformance optimalisations. I've replaced those scripts with the Destroy(..);
method to make it easier to understand.
Thanks in advance, Tim Visée
Could you be a little more specific and clear about what you want to achieve and what you have already tried?
Hey, yes. I've updated the question above, I hope I explained it well now.
From what I can see there is nothing wrong with the approach you are suggesting. I don't understand what your problem is?
Your answer
Follow this Question
Related Questions
Can I make a trailrenderer with a blur effect? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Detect closed loop in TrailRenderer and get gameObjects inside 2 Answers
Trails in just one direction 1 Answer