Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by timvisee · Jun 28, 2013 at 10:33 PM · c#rendertrailrenderertrail

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jun 29, 2013 at 03:09 AM 0
Share

Could you be a little more specific and clear about what you want to achieve and what you have already tried?

avatar image timvisee · Jun 29, 2013 at 10:06 AM 0
Share

Hey, yes. I've updated the question above, I hope I explained it well now.

avatar image Benproductions1 · Jun 29, 2013 at 11:51 AM 0
Share

From what I can see there is nothing wrong with the approach you are suggesting. I don't understand what your problem is?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges