- Home /
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
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
it would be easyer to make the trail with the particle system and just make it render world space and add more particles
Ok I tried some things with particle but I can't get it to behave like a trail really.
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.
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.
Your answer
Follow this Question
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