yotiky Tech Blog

とあるエンジニアの備忘録

Unity - スクリプトで LineRenderer を使って線を引く

        var head = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        head.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        head.transform.parent = transform;
        gazeLine = head.AddComponent<LineRenderer>();
        gazeLine.material = new Material(Shader.Find("Sprites/Default"));
        gazeLine.startWidth = 0.01f;
        gazeLine.endWidth = 0.01f;
        gazeLine.startColor = Color.blue;
        gazeLine.endColor = new Color(1, 0, 0, 0);
        gazeLine.SetPosition(0, head.transform.position);
        gazeLine.SetPosition(1, head.transform.forward * 2f);
        var point = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        point.transform.localScale = new Vector3(0.02f, 0.02f, 0.02f);
        point.transform.position = head.transform.forward * 2f;
        point.transform.parent = transform;
        point.GetComponent<Renderer>().material.color = Color.red;

f:id:yotiky:20200903203614p:plain