Advertisement
nebbul

TrailTravelTile

Sep 18th, 2018
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class TrailTravelTile : MonoBehaviour {
  6.     public int materialIndex = 0;
  7.     public Vector2 unitTiling = new Vector2( 1.0f, 1.0f );
  8.  
  9.     float uvOffset = 0.0f;
  10.    
  11.     public bool randomStartU = false;
  12.  
  13.     public List<string> textureList = new List<string>();
  14.  
  15.    
  16.     Vector3 lastWorldPosition;
  17.     float distanceTravelled;
  18.  
  19.     Material material;
  20.  
  21.     // Use this for initialization
  22.     void OnEnable () {
  23.         lastWorldPosition = transform.position;
  24.         distanceTravelled = 0;
  25.         if(randomStartU)
  26.         {
  27.             uvOffset = Random.Range(0.0f,1.0f);
  28.         }
  29.  
  30.         if (GetComponent<Renderer>() == null)
  31.         {
  32.             Debug.LogError("Failed to get renderer on: " + gameObject, gameObject);
  33.             enabled = false;
  34.             return;
  35.         }
  36.         material = (GetComponent<Renderer>().materials != null && GetComponent<Renderer>().materials.Length > materialIndex) ? GetComponent<Renderer>().materials[materialIndex] : null;
  37.         if (!material)
  38.         {
  39.             Debug.LogError("Failed to get Material: " + gameObject, gameObject);
  40.             enabled = false;
  41.             return;
  42.         }
  43.     }
  44.  
  45.     // Update is called once per frame
  46.     void LateUpdate () {
  47.         if(transform.position != lastWorldPosition){
  48.             Vector3 distanceRef = lastWorldPosition - transform.position;
  49.  
  50.             distanceTravelled += Mathf.Abs(Vector3.Magnitude(distanceRef));
  51.  
  52.             if (GetComponent<Renderer>() && GetComponent<Renderer>().enabled && material)
  53.             {
  54.                 Vector2 xyScale = new Vector2(distanceTravelled * unitTiling.x, unitTiling.y);
  55.                 Vector2 xyOffset = new Vector2((1-(xyScale.x - Mathf.Floor (xyScale.x))) + uvOffset, 0); // make it stretch from opposite side of trail. Also randomize offset U
  56.  
  57.                 for (int i = 0; i < textureList.Count; i++){
  58.                     material.SetTextureScale(textureList[i], xyScale);
  59.                     material.SetTextureOffset(textureList[i], xyOffset);
  60.                 }
  61.             }
  62.             lastWorldPosition = transform.position;
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement