Advertisement
Guest User

Speedlines

a guest
Feb 17th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class Speedlines : MonoBehaviour
  5. {
  6.     public Material material;
  7.  
  8.     // Start is called before the first frame update
  9.     void Start()
  10.     {
  11.         camera = Camera.main;
  12.         camera.orthographic = true;
  13.         camera.backgroundColor = Color.black;
  14.         camera.clearFlags = CameraClearFlags.Color;
  15.  
  16.         Vector3 topLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 1, 0));
  17.         Vector3 bottomLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 0, 0));
  18.         Vector3 bottomRightCorner = camera.ViewportToWorldPoint(new Vector3(1, 0, 0));
  19.  
  20.         float width = Vector3.Distance(bottomLeftCorner, bottomRightCorner);
  21.         float height = Vector3.Distance(bottomLeftCorner, topLeftCorner);
  22.  
  23.         int horizontalCount = Mathf.RoundToInt(width * 2) * 2;
  24.         int verticalCount = Mathf.RoundToInt(height * 2) * 2;
  25.  
  26.         for (int i = 0; horizontalCount + verticalCount > i; i++)
  27.         {
  28.             TrailRenderer trail = new GameObject("Trail").AddComponent<TrailRenderer>();
  29.             trail.material = material;
  30.             trails.Add(trail);
  31.         }
  32.     }
  33.  
  34.     // Update is called once per frame
  35.     void Update()
  36.     {
  37.         bool run = Input.GetMouseButton(0);
  38.  
  39.         Vector3 mousePosition = Input.mousePosition;
  40.         Vector3 worldMousePosition = camera.ScreenToWorldPoint(mousePosition);
  41.  
  42.         Vector3 newCameraPosition = camera.transform.position;
  43.  
  44.         newCameraPosition = Vector3.Lerp(worldMousePosition, newCameraPosition, Mathf.Pow(2, -10 * Time.deltaTime));
  45.  
  46.         camera.transform.position = newCameraPosition;
  47.  
  48.         Vector3 direction = Vector3.Normalize(worldMousePosition - newCameraPosition);
  49.  
  50.         Vector3 topLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 1, 0));
  51.         Vector3 bottomLeftCorner = camera.ViewportToWorldPoint(new Vector3(0, 0, 0));
  52.         Vector3 bottomRightCorner = camera.ViewportToWorldPoint(new Vector3(1, 0, 0));
  53.         Vector3 center = camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, -1));
  54.  
  55.         float width = Vector3.Distance(bottomLeftCorner, bottomRightCorner);
  56.         float height = Vector3.Distance(bottomLeftCorner, topLeftCorner);
  57.  
  58.         int horizontalCount = Mathf.RoundToInt(width) * 2;
  59.         int verticalCount = Mathf.RoundToInt(height) * 2;
  60.  
  61.         if (run)
  62.         {
  63.             HandleSpeedlines(0, horizontalCount, bottomLeftCorner, direction, center, new Vector3(1, 0, 0), 0.5f);
  64.             HandleSpeedlines(horizontalCount, horizontalCount * 2, topLeftCorner, direction, center, new Vector3(1, 0, 0), 0.5f);
  65.             HandleSpeedlines((horizontalCount * 2) + verticalCount, (horizontalCount * 2) + (verticalCount * 2), bottomRightCorner, direction, center, new Vector3(0, 1, 0), 0.5f);
  66.             HandleSpeedlines(horizontalCount * 2, (horizontalCount * 2) + verticalCount, bottomLeftCorner, direction, center, new Vector3(0, 1, 0), 0.5f);
  67.         }
  68.     }
  69.  
  70.     private void HandleSpeedlines(int from, int to, Vector3 initialPosition, Vector3 direction, Vector3 center, Vector3 offset, float densityMultiplier)
  71.     {
  72.         for (int i = from; to > i; i++)
  73.         {
  74.             Vector3 position = initialPosition + (offset * (i - from) * densityMultiplier) + new Vector3(0, 0, 1);
  75.  
  76.             float dot = Mathf.Clamp01(Normalize(Mathf.Clamp01(Vector3.Dot(direction, Vector3.Normalize(position - center))), 0.75f, 1));
  77.             float sin = Mathf.Sin((Mathf.Repeat(Time.time, 10) + dot) * 15);
  78.             float cos = Mathf.Cos((Mathf.Repeat(Time.time, 10) - dot) * 15);
  79.  
  80.             trails[i].time = dot;
  81.             trails[i].transform.position = position;
  82.             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));
  83.         }
  84.     }
  85.  
  86.     private float Normalize(float value, float min, float max) => (value - min) / (max - min);
  87.    
  88.     private new Camera camera;
  89.     private List<TrailRenderer> trails = new List<TrailRenderer>();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement