Advertisement
Guest User

thing

a guest
Jun 7th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AutoMove : MonoBehaviour {
  6.  
  7.     [System.Serializable]
  8.     public struct moveStep{public Vector3 pos; public Vector3 rot; [Range(0,1)]public float speed;public bool worldSpace; public bool smooth;}
  9.  
  10.     public GameObject movingObj;
  11.     public moveStep[] steps;
  12.  
  13.  
  14.  
  15.     int i =0;
  16.     public float speed;
  17.     public float lt;
  18.     public bool smooth;
  19.  
  20.  
  21.  
  22.     public bool loop;
  23.     public bool flip;
  24.     public bool worldSpace;
  25.  
  26.     public bool pi;
  27.  
  28.     public bool mode;
  29.     [Range(0,3)]
  30.     public float pim;
  31.     public float t;
  32.     // Use this for initialization
  33.     void Start () {
  34.         speed = steps [0].speed;
  35.         smooth = steps [0].smooth;
  36.         worldSpace = steps [0].worldSpace;
  37.  
  38.     }
  39.  
  40.     // Update is called once per frame
  41.     void Update () {
  42.  
  43.         Debug.DrawLine (transform.position, movingObj.transform.position);
  44.         Debug.DrawLine (transform.position, transform.position + steps [i].pos);
  45.         Debug.DrawLine (movingObj.transform.position, transform.position + steps [i].pos);
  46.         Debug.DrawLine (movingObj.transform.position, movingObj.transform.position + steps [i].pos);
  47.         if (i + 1 < steps.Length && steps [i + 1].pos != null) {
  48.             Debug.DrawLine (transform.position, transform.position + steps [i+ 1].pos);
  49.  
  50.         }
  51.         //if (steps[i-1].pos !=null)
  52.         //Debug.DrawLine (transform.position, transform.position + steps [i-1].pos);
  53.  
  54.  
  55.         lt += Time.deltaTime;
  56.         if (lt > speed)
  57.             lt = speed;
  58.         //lt = lt * lt;
  59.  
  60.         //float t =0f;
  61.         if (mode) {
  62.              t = lt / (speed);
  63.             if(pi)
  64.                 t = Mathf.Sin(t * Mathf.PI * pim);
  65.         }
  66.  
  67.         if (!mode) {
  68.              t = lt;
  69.             if(pi)
  70.                 t = Mathf.Sin(t * Mathf.PI * pim);
  71.         }
  72.         //multiplier += .2f * Time.smoothDeltaTime;
  73.  
  74.  
  75.  
  76.  
  77.         if (!smooth) {
  78.             movingObj.transform.localPosition = Vector3.Lerp (movingObj.transform.localPosition, steps [i].pos, t);
  79.             movingObj.transform.localRotation = Quaternion.Lerp (movingObj.transform.localRotation, Quaternion.Euler (steps [i].rot), t);
  80.  
  81.         }
  82.  
  83.         if (smooth) {
  84.  
  85.  
  86.             if (!worldSpace) {
  87.                 movingObj.transform.localPosition = Vector3.Slerp (movingObj.transform.localPosition, steps [i].pos, t);
  88.                 movingObj.transform.localRotation = Quaternion.Slerp (movingObj.transform.localRotation, Quaternion.Euler (steps [i].rot), t);
  89.  
  90.             } else {
  91.  
  92.                 movingObj.transform.position = Vector3.Slerp (movingObj.transform.localPosition, steps [i].pos, t);
  93.                 movingObj.transform.rotation = Quaternion.Slerp (movingObj.transform.localRotation, Quaternion.Euler (steps [i].rot), t);
  94.  
  95.             }
  96.         }
  97.  
  98.  
  99.  
  100.         ////////////////////////////////
  101.  
  102.         if (!worldSpace) {
  103.             if (Vector3.Distance (movingObj.transform.localPosition, steps [i].pos) <= .1f) {
  104.  
  105.                 Next ();
  106.                 lt = 0f;
  107.  
  108.  
  109.             }
  110.             /////////////////////////////////////
  111.         } else {
  112.             if (Vector3.Distance (movingObj.transform.position, steps [i].pos) <= .1f) {
  113.  
  114.                 Next ();
  115.                 lt = 0f;
  116.  
  117.  
  118.             }
  119.  
  120.         }
  121.  
  122.     }
  123.  
  124.  
  125.     void Next(){
  126.         ///if flip is true, we need to add to i, up to the length of the list.
  127.         /// otherwise, we need to substract from it, making sure it doesnt go below 0.
  128.         ///
  129.         ///
  130.         ///
  131.         ///
  132.         /// We need to add to i when going forward,
  133.         /// and subtract from it going back.
  134.         ///
  135.         /// we need to keep track of this, by flipping the value when we've reached length-1, also flipping it when reaching 0.
  136.         ///
  137.  
  138.  
  139.         //if (i > 0) { 
  140.         //  flip = false;
  141.  
  142.         //}
  143.  
  144.         //if (i < 0)
  145.         //  flip = true;
  146.  
  147.  
  148.         if (!loop) {
  149.  
  150.             if (flip) {
  151.                 i++;
  152.             } else {
  153.                 i--;
  154.  
  155.  
  156.             }
  157.  
  158.             if (i == steps.Length - 1 || i == 0) {
  159.                 flip = !flip;
  160.             }
  161.         } else {
  162.             if (i == steps.Length - 1) {
  163.                 i = 0;
  164.  
  165.  
  166.             } else {
  167.                 i++;
  168.             }
  169.         }
  170.  
  171.  
  172.  
  173.         //This happens in Next without conditionals
  174.         speed=  steps[i].speed;
  175.         smooth = steps [i].smooth;
  176.         worldSpace = steps [i].worldSpace;
  177.     }
  178.  
  179.  
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement