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 romitraj · Aug 14, 2014 at 06:44 AM · meshtrail renderertrails

Getting smoother trails in Unity

Hello,

I have created a small setup that creates harmonic motion. It is fairly simple. The problem is that I would like the objects moving with harmonic motion to have smooth trails but the trail renderer somehow makes jagged and ugly trails. Here is the code

Harmonizer.js is on a gameobject

 #pragma strict
 
 public var redRound:Texture2D;
 public var audioClip:AudioClip;
 public var delayComp:int=5;
 public var lineMaterial:Material;
 public var physicalObject:GameObject;
 public var distanceFromCamera:float;
 public var numPendulums = 60; 
 public var paren_t:GameObject;
 
 private var delay:int=0;
 private var pendulums:Pendulum[];
 private var startTime:float;
 private var musicScale:int[];
 private var scaleNoteCount = 5; 
 private var drawArray:RectCol[];
 private var drawArrayUpdate:boolean = false;
 private var vl:VectorLine;
 
 function Start () 
 {
     musicScale = new int[scaleNoteCount];
     musicScale[0] = 0;
     musicScale[1] = 3;
     musicScale[2] = 5;
     musicScale[3] = 7;
     musicScale[4] = 10;
     pendulums = new Pendulum[numPendulums];
     
     for(var i:int=0; i<numPendulums; i++)
     {
         var freq:float = (51.0+i)/60.0;
         //var midiNote:int = musicScale[i%scaleNoteCount] + (i/scaleNoteCount)*12 + 48;
         var midiNote:int =(musicScale[i%scaleNoteCount] + 12*(Mathf.Floor(i/scaleNoteCount)));
                         
         
         var gm:GameObject = new GameObject(""+i);
         gm.transform.position = new Vector3(0,0,0);
         gm.transform.rotation = Quaternion.identity;
         gm.AddComponent(AudioSource);
         var aSource:AudioSource = gm.GetComponent(AudioSource);
         aSource.clip = audioClip;
         aSource.playOnAwake = false;
         aSource.volume = 0.00;
     
         var gm2:GameObject = Instantiate(physicalObject, Vector3.zero, Quaternion.identity);
         gm2.transform.parent = paren_t.transform;
         
         pendulums[i] = new Pendulum(i,freq,midiNote,aSource, gm2);
         pendulums[i].UpdateScreenWidth(Screen.width);
         startTime = Time.time;
     }
     
     Time.timeScale = 0.25;
 }
 
 function FixedUpdate () 
 {
     if(delay >= delayComp)
     {
         delay = 0;
         var pendulumSize:float = Screen.height * 2.0 / numPendulums;
         var secs:float = Time.time-startTime;
         
         drawArray = new RectCol[numPendulums];
         drawArrayUpdate = true;
         
         for(var i:int=0; i<numPendulums; i++)
         {
             pendulums[i].PendulumUpdate(secs);
             drawArray[i] = pendulums[i].PendulumDraw(map(i,0,numPendulums-1, pendulumSize/2, Screen.height*2-pendulumSize/2),pendulumSize);
             //var tempRectCol:RectCol = pendulums[i].PendulumDraw(map(i,0,numPendulums-1, pendulumSize/2, Screen.height-pendulumSize/2),pendulumSize);    
         }
         
         
     }
     else
     {
         delay++;
     }
 
 }
 
 
 function OnGUI()
 {
     if(drawArray != null && drawArray.Length != 0)
     {
         for(var i:int=0; i<drawArray.Length; i++)
         {
             //GUI.color = drawArray[i].col;
             //GUI.DrawTexture(drawArray[i].rect,redRound);
             
             var worldPos:Vector3 = camera.main.ScreenToWorldPoint(new Vector3(drawArray[i].rect.x,drawArray[i].rect.y,paren_t.transform.position.z));
             pendulums[i].pObjects.transform.localPosition = worldPos;
             var s_cale:float = map(drawArray[i].rect.width, 0, Screen.width, 1.0, 10.0);
             pendulums[i].pObjects.transform.localScale = new Vector3(s_cale,s_cale,s_cale);
             pendulums[i].pObjects.renderer.material.color = drawArray[i].col;
             
             //Debug.Log(drawArray[i].rect.width);
             //Debug.Log(drawArray[i].rect.height);
         }
     }
 }
 
 function map(val:float, from1:float, to1:float, from2:float, to2:float)
 {
      return (val - from1) / (to1 - from1) * (to2 - from2) + from2;
 }

There a small Pendulum class as well

 class Pendulum
 {
     private var index:int;
     private var frequency:float;
     private var velocity:float;
     private var pitch:int;
     private var hit:float;
     private var pos:float;
     private var wid:float;
     private var aSource:AudioSource;
     public var pObjects:GameObject;
     
     function Pendulum(_index:int, _frequency:float, _pitch:int, _aSource:AudioSource, _pObjects:GameObject)
     {
         index = _index;
         frequency = _frequency;
         pitch = _pitch;
         aSource = _aSource;
         pObjects = _pObjects;
         
         pos = 1.0;
         velocity = 0.0;
         hit = 0.0;
     }
     
     function UpdateScreenWidth(_wid:float)
     {
         wid = _wid;
     }
     
     function PendulumUpdate(t:float)
     {
         var oldPos:float = pos;
         var oldVelocity:float = velocity;
         
         pos = Mathf.Cos(frequency*t*2*Mathf.PI);
         velocity = pos - oldPos;
         
         if(velocity * oldVelocity <= 0.0)
         {
             PlayNote(pitch, pos < 0 ? 0 : 127);
             hit = 1;
         }
         else
         {
             hit *= 0.85;
         }
     }
     
     function PendulumDraw(y:float, size:float)
     {    
         var r:float = Mathf.Lerp(255,0,hit);
         var g:float = Mathf.Lerp(255,0,hit);
         var b:float = Mathf.Lerp(255,0,hit);
         var a:float = Mathf.Lerp(0.5,1.0,hit);
         
         var col:Color = new Color(r/255.0,g/255.0,b/255.0,a);
         var rect:Rect = new Rect(map(pos, -1.0, 1.0, size/2.0, wid/1.0), y, size, size);
         
         var rc:RectCol = new RectCol(rect, col);
         return rc;
     }
     
     function PlayNote(_pitch:int, _pan:int)
     {
         aSource.pitch = map(_pitch,0,34,0,1);
         aSource.panLevel = map(_pan,0,127,0,1);
         aSource.Play();
     }
     
     function map(val:float, from1:float, to1:float, from2:float, to2:float)
     {
          return (val - from1) / (to1 - from1) * (to2 - from2) + from2;
     }
 }

Here is an image of the mess that Unity trail renderer is creating

Messy Trails

Is there a way I can make this smooth without creating a new trail system. Also if I am creating a new trail system, how would I go about doing that. Would I have to figure out how to create mesh over splines? Any help would be great

messytrail.jpg (292.7 kB)
Comment
Add comment · Show 5
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 RedDevil · Aug 14, 2014 at 07:00 AM 0
Share

it would be easyer to make the trail with the particle system and just make it render world space and add more particles

avatar image romitraj · Aug 14, 2014 at 07:17 AM 0
Share

Hmm... that is interesting. Let me try and report back.

avatar image romitraj · Aug 14, 2014 at 07:29 AM 0
Share

Ok I tried some things with particle but I can't get it to behave like a trail really.

avatar image robertbu · Aug 14, 2014 at 08:11 AM 0
Share

There are two trail systems and an Arc Renderer in the Unity Wiki. Google "Unity3d Wiki trail". For a bit of money, try using Vectorsity from the Asset store. You could define your trails as lines.

avatar image romitraj · Aug 14, 2014 at 09:50 AM 0
Share

Thanks for that robert. I gave the trail system in Wiki a go and it seems to work better. I am going have a look at Vectorsity.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trail renderer looks weird when there is a change of direction 1 Answer

Black Trail Rendering in 2D game 1 Answer

Any way to pause the Trail Renderer ? 1 Answer

Trails in just one direction 1 Answer

Trail renderer on top of player object 0 Answers


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