Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class TrailTravelTile : MonoBehaviour {
- public int materialIndex = 0;
- public Vector2 unitTiling = new Vector2( 1.0f, 1.0f );
- float uvOffset = 0.0f;
- public bool randomStartU = false;
- public List<string> textureList = new List<string>();
- Vector3 lastWorldPosition;
- float distanceTravelled;
- Material material;
- // Use this for initialization
- void OnEnable () {
- lastWorldPosition = transform.position;
- distanceTravelled = 0;
- if(randomStartU)
- {
- uvOffset = Random.Range(0.0f,1.0f);
- }
- if (GetComponent<Renderer>() == null)
- {
- Debug.LogError("Failed to get renderer on: " + gameObject, gameObject);
- enabled = false;
- return;
- }
- material = (GetComponent<Renderer>().materials != null && GetComponent<Renderer>().materials.Length > materialIndex) ? GetComponent<Renderer>().materials[materialIndex] : null;
- if (!material)
- {
- Debug.LogError("Failed to get Material: " + gameObject, gameObject);
- enabled = false;
- return;
- }
- }
- // Update is called once per frame
- void LateUpdate () {
- if(transform.position != lastWorldPosition){
- Vector3 distanceRef = lastWorldPosition - transform.position;
- distanceTravelled += Mathf.Abs(Vector3.Magnitude(distanceRef));
- if (GetComponent<Renderer>() && GetComponent<Renderer>().enabled && material)
- {
- Vector2 xyScale = new Vector2(distanceTravelled * unitTiling.x, unitTiling.y);
- 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
- for (int i = 0; i < textureList.Count; i++){
- material.SetTextureScale(textureList[i], xyScale);
- material.SetTextureOffset(textureList[i], xyOffset);
- }
- }
- lastWorldPosition = transform.position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement