Advertisement
Guest User

Speedlines

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