0
\$\begingroup\$

So I'm trying to render a line that shows up when camera sees an image target and moves around with it. Very typical AR application. But my line renderer doesn't seem to show at all.

Here's the code:

using UnityEngine;
using System.Collections;

public class myLine : MonoBehaviour
{
    // Creates a line renderer that follows a Sin() function
    // and animates it.

    public Color c1 = Color.yellow;
    public Color c2 = Color.red;
    public int lengthOfLineRenderer = 20;

    void Start()
    {
        LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
        lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
        lineRenderer.widthMultiplier = 0.2f;
        lineRenderer.positionCount = lengthOfLineRenderer;

        // A simple 2 color gradient with a fixed alpha of 1.0f.
        float alpha = 1.0f;
        Gradient gradient = new Gradient();
        gradient.SetKeys(
            new GradientColorKey[] { new GradientColorKey(c1, 0.0f), new GradientColorKey(c2, 1.0f) },
            new GradientAlphaKey[] { new GradientAlphaKey(alpha, 0.0f), new GradientAlphaKey(alpha, 1.0f) }
            );
        lineRenderer.colorGradient = gradient;
    }

    void Update()
    {
        LineRenderer lineRenderer = GetComponent<LineRenderer>();
        var t = Time.time;
        for (int i = 0; i < lengthOfLineRenderer; i++)
        {
            lineRenderer.SetPosition(i, new Vector3(i * 0.5f, Mathf.Sin(i + t), 0.0f));
        }
    }
}

and here's my set up:

enter image description here

I also tried attaching the script directly to the ImageTarget and nothing happened.

\$\endgroup\$
  • \$\begingroup\$ I think the problem here is that the camera is moving around in the scene to match orientation with your image target. But what you want is for the camera to be fixed and have the 3D model move around to match the image target. Am I correct? If so, you need to select the camera and change the "World Center Mode" to....Camera, I believe. \$\endgroup\$ – Draco18s no longer trusts SE Jun 22 '17 at 20:09
  • \$\begingroup\$ @Draco18s No i'm moving the camera around and have my target fixed. I want the line to show up on top of my image target, but it doesn't. \$\endgroup\$ – user86861 Jun 22 '17 at 20:23
  • \$\begingroup\$ Check that your line is being draw in local coordinates, not world, especially if your image target isn't placed at the origin. You may also wish to scale the coordinates you're setting so that it's larger. \$\endgroup\$ – Draco18s no longer trusts SE Jun 22 '17 at 20:30
  • \$\begingroup\$ @Draco18s It seems to renders on PC but not on my device. only shows a pink square instead of what it's supposed to show. \$\endgroup\$ – user86861 Jun 22 '17 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy