【Unity】TrailRendererで線を描く
TrailRendererを使えば、オブジェクトが動いた時の軌跡を描くことができます。 今回は関数で描いた線を表示するコードです。 (スクリプトリファレンスに載っているサンプルを少し変えたものです。)
TrailRendererのてすと。 pic.twitter.com/CCtl7nNCzJ
— miyamo (@DMiyamo3) 2017年7月19日
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TrailSample1: MonoBehaviour { bool isMenu = true; public float time = 1.0f; public float width = 0.1f; public float r = 0; public float g = 0; public float b = 0; Color color = Color.black; TrailRenderer tr; // Use this for initialization void Start () { tr = GetComponent<TrailRenderer>(); tr.material = new Material(Shader.Find("Sprites/Default")); } // Update is called once per frame void Update () { tr.time = time; tr.startWidth = width; tr.endWidth = width; color = new Color(r, g, b); tr.startColor = color; tr.endColor = color; tr.transform.position = new Vector3(Mathf.Sin(Time.time * 1.51f) * 7.0f, Mathf.Cos(Time.time * 1.27f) * 4.0f, 0f); } void OnGUI() { if(GUI.Button(new Rect(25, 5, 50, 15), "Menu")) { isMenu = !(isMenu); } if(isMenu == true) { GUI.Label(new Rect(25, 20, 200, 30), "Time"); time = GUI.HorizontalSlider(new Rect(125, 25, 200, 30), time, 0.1f, 100.0f); GUI.Label(new Rect(25, 50, 200, 60), "Width"); width = GUI.HorizontalSlider(new Rect(125, 55, 200, 60), width, 0.01f, 1.0f); GUI.Label(new Rect(25, 80, 200, 90), "Color"); r = GUI.HorizontalSlider(new Rect(125, 90, 200, 60), r, 0f, 1.0f); g = GUI.HorizontalSlider(new Rect(125, 120, 200, 60), g, 0f, 1.0f); b = GUI.HorizontalSlider(new Rect(125, 150, 200, 60), b, 0f, 1.0f); } } }