Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class Speedlines : MonoBehaviour
- {
- public Material material;
- // Start is called before the first frame update
- void Start()
- {
- camera = Camera.main;
- camera.orthographic = true;
- camera.backgroundColor = Color.black;
- camera.clearFlags = CameraClearFlags.Color;
- Vector3 topLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 1, 0));
- Vector3 bottomLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 0, 0));
- Vector3 bottomRightCorner = camera.ViewportToWorldPoint(new Vector3(1, 0, 0));
- float width = Vector3.Distance(bottomLeftCorner, bottomRightCorner);
- float height = Vector3.Distance(bottomLeftCorner, topLeftCorner);
- int horizontalCount = Mathf.RoundToInt(width * 2) * 2;
- int verticalCount = Mathf.RoundToInt(height * 2) * 2;
- for (int i = 0; horizontalCount + verticalCount > i; i++)
- {
- TrailRenderer trail = new GameObject("Trail").AddComponent<TrailRenderer>();
- trail.material = material;
- trails.Add(trail);
- }
- }
- // Update is called once per frame
- void Update()
- {
- bool run = Input.GetMouseButton(0);
- Vector3 mousePosition = Input.mousePosition;
- Vector3 worldMousePosition = camera.ScreenToWorldPoint(mousePosition);
- Vector3 newCameraPosition = camera.transform.position;
- newCameraPosition = Vector3.Lerp(worldMousePosition, newCameraPosition, Mathf.Pow(2, -10 * Time.deltaTime));
- camera.transform.position = newCameraPosition;
- Vector3 direction = Vector3.Normalize(worldMousePosition - newCameraPosition);
- Vector3 topLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 1, 0));
- Vector3 bottomLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 0, 0));
- Vector3 bottomRightCorner = camera.ViewportToWorldPoint(new Vector3(1, 0, 0));
- Vector3 center = camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, -1));
- float width = Vector3.Distance(bottomLeftCorner, bottomRightCorner);
- float height = Vector3.Distance(bottomLeftCorner, topLeftCorner);
- int horizontalCount = Mathf.RoundToInt(width) * 2;
- int verticalCount = Mathf.RoundToInt(height) * 2;
- if (run)
- {
- HandleSpeedlines(0, horizontalCount, bottomLeftCorner, direction, center, new Vector3(1, 0, 0), 0.5f);
- HandleSpeedlines(horizontalCount, horizontalCount * 2, topLeftCorner, direction, center, new Vector3(1, 0, 0), 0.5f);
- HandleSpeedlines((horizontalCount * 2) + verticalCount, (horizontalCount * 2) + (verticalCount * 2), bottomRightCorner, direction, center, new Vector3(0, 1, 0), 0.5f);
- HandleSpeedlines(horizontalCount * 2, (horizontalCount * 2) + verticalCount, bottomLeftCorner, direction, center, new Vector3(0, 1, 0), 0.5f);
- }
- }
- private void HandleSpeedlines(int from, int to, Vector3 initialPosition, Vector3 direction, Vector3 center, Vector3 offset, float densityMultiplier)
- {
- for (int i = from; to > i; i++)
- {
- Vector3 position = initialPosition + (offset * (i - from) * densityMultiplier) + new Vector3(0, 0, 1);
- float dot = Mathf.Clamp01(Normalize(Mathf.Clamp01(Vector3.Dot(direction, Vector3.Normalize(position - center))), 0.75f, 1));
- float sin = Mathf.Sin((Mathf.Repeat(Time.time, 10) + dot) * 15);
- float cos = Mathf.Cos((Mathf.Repeat(Time.time, 10) - dot) * 15);
- trails[i].time = dot;
- trails[i].transform.position = position;
- trails[i].widthCurve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(0.4f * ((sin + 1) * 0.5f), (sin + 1) * 0.5f * Random.Range(0.05f, 0.25f) * dot), new Keyframe(0.4f + (0.4f * ((cos + 1) * 0.5f)), 0));
- }
- }
- private float Normalize(float value, float min, float max) => (value - min) / (max - min);
- private new Camera camera;
- private List<TrailRenderer> trails = new List<TrailRenderer>();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement