Created
October 17, 2017 18:48
-
-
Save Maarrk/084d7a7a6662bf21c31df4d81584e0d2 to your computer and use it in GitHub Desktop.
Skrypt do wykładu "Move ya body" w KNTG Polygon 18.10.2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
// Skrypt do wykładu "Move ya body" w KNTG Polygon 18.10.2017 | |
[RequireComponent(typeof(LineRenderer))] | |
public class InputChart : MonoBehaviour { | |
// Dla domyślnej kamery w projekcie 2D wystarczy wrzucić ten GameObject na pozycję (-8, 0, 0) | |
public int pointsCount = 80; | |
public float scaleY = 4f; | |
public float scaleX = 0.2f; | |
public string axisName = "Vertical"; | |
private int _pointsCount; | |
private LineRenderer linRend; | |
private Color lineColor = Color.white; | |
private Vector3[] points; | |
void Awake() | |
{ | |
// Przepisz do prywatnej zmiennej żeby nie zmienić jej w edytorze | |
_pointsCount = pointsCount; | |
points = new Vector3[_pointsCount]; | |
} | |
void Start() | |
{ | |
// Ustaw LineRenderer | |
linRend = GetComponent<LineRenderer>(); | |
linRend.positionCount = _pointsCount; | |
linRend.useWorldSpace = false; | |
linRend.startWidth = scaleY * 0.1f; | |
linRend.endWidth = scaleY * 0.05f; | |
linRend.startColor = lineColor; | |
linRend.endColor = lineColor; | |
} | |
void Update () { | |
// Przesuń wszystkie wysokości jedno pole dalej | |
for (int i = _pointsCount - 1; i >= 1; i--) | |
{ | |
points[i] = points[i - 1]; | |
points[i].x += scaleX; | |
} | |
// Ustaw kolor początku linii | |
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.S)) | |
{ | |
lineColor = Color.yellow; | |
} | |
else if (Input.GetKey(KeyCode.W)) | |
{ | |
lineColor = Color.green; | |
} | |
else if (Input.GetKey(KeyCode.S)) | |
{ | |
lineColor = Color.red; | |
} | |
else | |
{ | |
lineColor = Color.white; | |
} | |
// Wpisz nowy input na początek | |
points[0] = new Vector3(0f, Input.GetAxis(axisName) * scaleY, 0f); | |
// Wyświetl wykres | |
linRend.SetPositions(points); | |
linRend.startColor = lineColor; | |
} | |
void OnDrawGizmos() | |
{ | |
// Pokazuje gdzie jest początek wykresu żeby było łatwiej ustawić w polu widzenia | |
Gizmos.color = lineColor; | |
Gizmos.DrawWireCube(transform.position, Vector3.one * scaleY * 0.2f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment