Log in or register to post comments

Draw line above image target

March 2, 2013 - 7:03am #1

Hello,

 

I need to draw a line between two objects above my imagetarget. I' ve read in unity docs and forums about how to draw lines and i ended up using 

Debug.DrawLine(Vector3 start, Vector3 end, color, float time);

When i play the application on unitys' editor the line doesn't show up and the same applies to the mobile device i am testing. But, if i press the pause button in unity then the line appears exactly as i expected.

Is there any advice on how to make it work on the device too?

A better approach on drawing lines is also welcome!

 

thank you in advance

Draw line above image target

March 6, 2013 - 2:48am #11

Based on your script i managed to make it work:

Here is the part of the code that is responsible for the line (Unity Script)

function start(){
//....
      lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
      lineRenderer.SetColors(c1, c2);
      lineRenderer.SetWidth(2,2);
      lineRenderer.transform.parent = target.transform;
//.....
}
function initDest(){
      json_data = jsonURL;
      www = new WWW (json_data);
      yield www;
      j = json.fromString(www.text);
      steps = j._get("routes")._get(0)._get("legs")._get(0)._get("steps").values.length;
     //one line with more than one vertexes
      lineRenderer.SetVertexCount(steps);
}
function update(){
//.....
      navigate();
//.....
}
function navigate(){
//.....
      //continuous line
      lineRenderer.SetPosition(i, Vector3(point1.X,1,point1.Y));
//.....
}

 

The initDest function is called using startCoroutine, otherwise it doesn't get past the yield command on the device.

Thanks!

 

Draw line above image target

March 3, 2013 - 10:47am #10

Here's a script that will accomplish what you're looking for using the LineRenderer class - Add a LineRenderer component (  Component > Effects ) to your ImageTarget instance in the Inspector. And add this script to the target as well, be sure to assign the GameObjects that you want to connect to the Obj A and B fields. This should work w/ any WCM. The LineRenderer will provide you a lot more flexibility in customizing the appearance of the line, and enable you to add line segments.

using UnityEngine;
using System.Collections;

public class DrawALine : MonoBehaviour {
    
    public GameObject objA;
    public GameObject objB;
    
    LineRenderer lineRenderer;
     
    void Awake()
    {
      lineRenderer = (LineRenderer) this.GetComponent(typeof(LineRenderer));
    }
    // Use this for initialization
    void Start () {
        
        if( objA== null || objB == null )
            throw new System.Exception("You must assign two GameObjects");
        

    }
    

    void Update () {
    
        lineRenderer.SetPosition(0, objA.transform.position );
        lineRenderer.SetPosition(1, objB.transform.position );

    }
}

 

Draw line above image target

March 2, 2013 - 10:54am #9

I've read that reference

From the two points the first is the location of a user (user prefab). These coordinates are updated as he moves. The second pair is the location (3d object prefab) he wants to go.

The json object i am parsing holds the route the user must follow in order to get to the desired location.

Both objects are children of my imagetarget and the user prefab updates its position succesfully while on the move.

What i am trying to achive is to connect the two points (user, 3d object) using lines and show the route.

The coordinates between the two prefabs are correct because when i pause the app the line appears exactly how it should be. My problem is that it's not visible while testing on the device. So, i thought that this happens because i'm not connecting somehow the lines with the imagetarget (parent - children).

 

The json holds lat - lon information which i have to convert them to my space coordinates in order to calculate everything correctly.

 

Draw line above image target

March 2, 2013 - 10:28am #8

Try using the transform.positions of your 3D objects for a demo of how DrawLine should work. You can still use DrawLine, but you've got to give it the correct coordinates.

What are you doing w/ the coordinates that you're importing - what's their purpose?

Also here are some alternatives to drawline - http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/

Draw line above image target

March 2, 2013 - 10:20am #7

Yes...you are so right! that was something i had in mind but i gave a try...so do i have to make the line a children of the imagetarget too?

I think that Debug.DrawLine will not help me? right?

 

thanks, you are really helpful!

 

 

 

Draw line above image target

March 2, 2013 - 10:10am #6

So you're not using the transforms of the objects, you're importing coordinates? - keep in mind that the objects are in the local space of the target if they are children of the target.

Draw line above image target

March 2, 2013 - 10:08am #5

Ok, i changed the code and now i am calling my function in update. Still the same!

posting sample in case you find something

function Update() {
//.....
connectPoints();
}

function connectPoints(){
  if (www.error == null)
  {
	var j:json = json.fromString(www.text);
       //........parse Json and get points

	point1 = convert(x1,y1);
        point2 = convert(x2,y2);
  }
  else
       Debug.Log("WWW Error: "+ www.error);

   Debug.drawLine(Vector3(x1,1,y1), Vector3(x2,1,y2), UnityEngine.Color.blue, 5);
}

I am parsing a json, calculate the points and calling draw line for each point.

 

Result: the line is visible only when i press the pause button in Unity.

Draw line above image target

March 2, 2013 - 9:13am #4

DrawLine needs to be called each frame - can you call your custom function from Update?

Draw line above image target

March 2, 2013 - 9:05am #3

No. Do i have to call it in update only? For the needs of the application, i 'm trying to do it from a custom function i created.

This is because i have to calculate points depending on users' actions. I will try it and reply.

I' ve seen LineRenderrer but i haven't tried it yet. Will try.

 

Thank you

Draw line above image target

March 2, 2013 - 8:13am #2

Are you calling DrawLine in Update?

You can also draw lines using the LineRenderer - http://docs.unity3d.com/Documentation/Components/class-LineRenderer.html

Log in or register to post comments