Hi ppl,
OK so I’m having a little bit of grief with trontrails and I’m not sure if its down to the way I’ve hacked the code around (although I did re-try using trontrails original and it still didn’t give me the results I was after)
I realise the code is a bit rough but here it is
var tickOver=0;
var myLine : Transform;
var height = 2.0;
var time = 2.0;
var alwaysUp = false;
var minDistance = 0.1;
var startColor = Color.white;
var endColor = Color (1, 1, 1, 0);
var position : Vector3;
var mytarget : Transform;
var sections = new Array();
var sectionLen =0;
var currentlyDown =false;
var hit : RaycastHit;
var currentlyPlanningSheep : Transform;
class TronTrailSection
{
var point : Vector3;
var upDir : Vector3;
var time : float;
}
function FixedUpdate () {
if(Input.GetMouseButtonDown(0)){
pos=Input.mousePosition;
if (Physics.Raycast(camera.main.ScreenPointToRay(pos), hit, 200)){
Debug.Log(hit.collider.transform.name);
if(hit.collider.transform){
if(hit.collider.transform.name=="topSheep"){
currentlyPlanningSheep=hit.collider.transform.parent;
currentlyDown=true;
}
else
{
Debug.Log(hit.collider.transform.parent.name);
}
}
}
}
if(Input.GetMouseButtonUp(0)){
//here we copy the the path/Mesh of this object to a new GO and assign it to the currentlyPlanningSheep
//we then clear our working copy of the path
//and clear our currentlyPlanningSheep assignment the flag
//copy working data to new and assign.
//reset working data
currentlyDown=false;
}
if(Input.GetMouseButton(0) currentlyDown)
{
mydepth=camera.main.transform.position.z-mytarget.position.z;
pos=Input.mousePosition;
if (Physics.Raycast(camera.main.ScreenPointToRay(pos), hit, 100)){
position = hit.point;
// Instantiate(myLine,position,myLine.rotation);
}
position = hit.point;
var now = Time.time;
// Remove old sections
/*
position.z-=5;
if (sections.length>0)var tsec2 :TronTrailSection =sections[sections.length-1];
Debug.Log(sections.length);
while (sections.length > 0 now > tsec2.time+time) {
sections.Pop();
}
*/
// Add a new trail section
//We Will need to modify this quite a bit to get it to create it in th right way
if(sections.length>0) var tsection :TronTrailSection =sections[0];
if ( sections.length == 0 || ( tsection.point - position).sqrMagnitude > minDistance * minDistance)
{
var section = TronTrailSection ();
section.point = position;
if (alwaysUp)
section.upDir = Vector3.up;
else
section.upDir = transform.TransformDirection(Vector3.up);
section.time = now;
sections.Unshift(section);
}
// Rebuild the mesh
var tmpMesh : MeshFilter =GetComponent(MeshFilter);
mesh = tmpMesh.mesh;
mesh.Clear();
// if (sections.length < 2)
// return;
//okso far
var vertices = new Vector3[sections.length * 4];
var colors = new Color[sections.length * 4];
var uv = new Vector2[sections.length * 4];
var previousSection : TronTrailSection = sections[0];
var currentSection : TronTrailSection = sections[0];
// Use matrix instead of transform.TransformPoint for performance reasons
var localSpaceTransform = transform.worldToLocalMatrix;
// Generate vertex, uv and colors
for (var i=0;i<sections.length;i++)
{
previousSection = currentSection;
currentSection = sections[i];
// Calculate u for texture uv and color interpolation
var u = 0.0;
if (i != 0)
u = Mathf.Clamp01 ((Time.time - currentSection.time) / time);
// Calculate upwards direction
var upDir = currentSection.upDir;
// Generate vertices
vertices[i * 4 + 0] = localSpaceTransform.MultiplyPoint(currentSection.point);
vertices[i * 4 + 1] = localSpaceTransform.MultiplyPoint(currentSection.point +Vector3.up * height);
vertices[i * 4 + 2] = localSpaceTransform.MultiplyPoint(currentSection.point +Vector3.right * height);
vertices[i * 4 + 3] = localSpaceTransform.MultiplyPoint(currentSection.point +(Vector3.up*height)+(Vector3.right * height));
uv[i * 4 + 0] = Vector2(u, 0);
uv[i * 4 + 1] = Vector2(u, 1);
uv[i * 4 + 2] = Vector2(u, 1);
uv[i * 4 + 3] = Vector2(u, 1);
// fade colors out over time
var interpolatedColor = startColor;
// var interpolatedColor = Color.Lerp(startColor, endColor, u);
colors[i * 4 + 0] = interpolatedColor;
colors[i * 4 + 1] = interpolatedColor;
colors[i * 4 + 2] = interpolatedColor;
colors[i * 4 + 3] = interpolatedColor;
}
// Generate triangles indices
var triangles = new int[(sections.length -1) * 4*3];
for (i=0;i<triangles.length/12;i++)
{
triangles[i * 12 + 0] = i * 3;
triangles[i * 12 + 1] = i * 3 + 1;
triangles[i * 12 + 2] = i * 3 + 2;
triangles[i * 12 + 3] = i * 3 + 2;
triangles[i * 12 + 4] = i * 3 + 1;
triangles[i * 12 + 5] = i * 3 + 3;
triangles[i * 12 + 6] = i * 3 + 2;
triangles[i * 12 + 7] = i * 3 + 3;
triangles[i * 12 + 8] = i * 3 + 4;
triangles[i * 12 + 9] = i * 3 + 5;
triangles[i * 12 + 10] = i * 3 + 4;
triangles[i * 12 + 11] = i * 3 + 3;
}
// Assign to mesh
mesh.vertices = vertices;
mesh.colors = colors;
mesh.uv = uv;
mesh.triangles = triangles;
Debug.Log("Triangles Length ="+triangles.length +" Sections ="+sections.length);
//Closing brace
}
}
Now I’m having 2 problems with this
1, the trail begins to disappear after a little bit of drawing it it ends up looking like a ‘snake’ from the old nokia games (and others) I don’t want it to disappear.
2. when going verticle or 1/2 the directions you could be drawing the ‘width’ of the line fails to be consistant, because its using ‘stretched’ triangles / squares to make up the line.
Is there an easier way of drawing a like and logging the points in space (trying to get a ‘draw race’ effect so that my other Game Object can get the path/mesh assigned to it so it can follow it.
[/quote]