x


Moving a single instance

I draw a line on which I want to add "handles", these "handles" can be moved and leave a line from their respective anchorpoint, to the new position of their respective "handle". So I made a prefab "handle" which gets instantiated on the line after a certain distance from the last "handle" was drawn. Each handle should be able to move independently, because I want to use the line length for other calculations. But when I have more than 1 instance, all overlap when I try to move a single "handle".

Here's some code to elaborate:

var rubberBand:GameObject;
private var uniqueRubberBand:GameObject;
private var rubberBandName:String;
uniqueRubberBand = Instantiate(rubberBand, Vector3(linepos.x, linepos.y + 5, linepos.z),  transform.rotation);
rubberBandName = "RubberBand";
rubberBandName = rubberBandName.Insert(rubberBandName.Length, rubberBandNameArray.length.ToString());

Now for the line drawing, that is happening in a script attached to the prefab:

function Start () 
{
    anchor = transform.position;
    drag = false;
    //Line1
    line = new LineRenderer();
    line = gameObject.AddComponent(LineRenderer);
    material = new Material(Shader.Find("Diffuse"));
    material.color = Color.red;
    line.material = material;
    line.SetVertexCount(0);
    line.SetWidth(startWidth, endWidth);
    //Line2
    lineHelper2 = new GameObject();
    lineHelper2.name = this.name + "lineHelper2";
    line2 = new LineRenderer();
    line2 = lineHelper2.AddComponent(LineRenderer);
    line2.SetVertexCount(0);
    line2.SetWidth(startWidth, endWidth);
    material2 = new Material(Shader.Find("Diffuse"));
    material2.color = Color.blue;
    line2.material = material2;
    //Line3
    ...
}
function Update()
{...
         line.SetVertexCount(2);
         line.SetPosition(0, anchor);
         line.SetPosition(1, anchor + unitVector * 25);
         line2.SetVertexCount(2);
         line2.SetPosition(0, anchor + unitVector * 25);
         line2.SetPosition(1, anchor + unitVector * 50);
         line3.SetVertexCount(2);
         line3.SetPosition(0, anchor + unitVector * 50);
         line3.SetPosition(1, transform.position);
    ...}
more ▼

asked Apr 05 '12 at 05:11 PM

Luci85 gravatar image

Luci85
143 7 14 16

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Just found the solution: Make a raycast and test if the hit is the instance itself and not something else.

if (Physics.Raycast (ray, hit, Mathf.Infinity))
if(hit.transform.name == this.name)
more ▼

answered Apr 05 '12 at 06:53 PM

Luci85 gravatar image

Luci85
143 7 14 16

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1288
x208
x15

asked: Apr 05 '12 at 05:11 PM

Seen: 353 times

Last Updated: Apr 05 '12 at 06:54 PM