[Unity] LineRenderer で描画した線を動かす
前回 script で LineRenderer を使って線を描画しました。
今回はその描画した線を動かしてみます。
実装方法
前回追加した空の GameObject に RigidBody 2D Component を追加して、script を以下のように書き換えます。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class LineController : MonoBehaviour { private GameObject obj; private Vector3 moveSpeed = new Vector3(0.001f, 0.001f, 0); void Start() { obj = gameObject; LineRenderer line = obj.AddComponent<LineRenderer>(); line.startWidth = 0.1f; line.endWidth = 0.1f; line.material = new Material(Shader.Find("Sprites/Default")); line.startColor = Color.red; line.endColor = Color.blue; line.SetPositions( new Vector3[] { new Vector3(-1, 0), new Vector3(1, 0), } ); } void FixedUpdate() { obj.transform.position += moveSpeed; } }
このように前回描画した線を移動させることができます。
FixedUpdate メソッド内で対象のオブジェクトの座標を変更することで描画した線を移動させています。