SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Moviment : MonoBehaviour {
  6.  
  7.     public bool teste;
  8.     public Vector3Int destiny;
  9.     SpriteRenderer sprite;
  10.     Transform jumper;
  11.     TileLogic tileAtual;
  12.  
  13.     void Awake() {
  14.         this.jumper = this.transform.Find("Jumper");
  15.         this.sprite = this.transform.GetComponentInChildren<SpriteRenderer>();
  16.     }
  17.  
  18.     // Update is called once per frame
  19.     void Update() {
  20.         if( teste ){
  21.             teste = false;
  22.             StopAllCoroutines();
  23.             StartCoroutine( Move() );  
  24.         }
  25.     }
  26.  
  27.     IEnumerator Move(){
  28.         yield return null;
  29.         TileLogic tileLogic =  Board.instance.tiles[ this.destiny ];
  30.  
  31.         Vector3 startPosition = this.transform.position;
  32.         Vector3 endPosition = tileLogic.worldPosition;
  33.         float totalTime = 1;
  34.         float tempTime = 0;
  35.  
  36.         if( this.tileAtual == null ){
  37.             this.tileAtual = tileLogic;
  38.         }
  39.         if( this.tileAtual.floor != tileLogic.floor ){
  40.             Debug.Log( "totalTime: " + totalTime );
  41.             StartCoroutine( Jumper( tileLogic, totalTime ) );
  42.         }
  43.  
  44.         while ( this.transform.position != endPosition ) {
  45.             tempTime += Time.deltaTime;
  46.             float porcentagem = tempTime / totalTime;
  47.             this.transform.position = Vector3.Lerp( startPosition, endPosition, porcentagem );
  48.             yield return null;
  49.         }
  50.  
  51.         Debug.Log( "Order: " + tileLogic.order );
  52.         sprite.sortingOrder = tileLogic.order;
  53.         tileLogic.content = this.gameObject;
  54.     }
  55.  
  56.     IEnumerator Jumper( TileLogic tileLogic, float totalTime ) {
  57.         Vector3 halfwayPosition; // = this.jumper.localPosition;
  58.         Vector3 startPosition = halfwayPosition = this.jumper.localPosition;
  59.         halfwayPosition.y += 0.5f;
  60.         float tempTime = 0;
  61.         while ( this.jumper.localPosition != halfwayPosition ) {
  62.             tempTime += Time.deltaTime;
  63.             float porcentagem = tempTime / ( totalTime * 0.5f ) ;
  64.             this.jumper.localPosition = Vector3.Lerp( startPosition, halfwayPosition, porcentagem );
  65.             yield return null;
  66.         }
  67.         tempTime = 0;
  68.         while ( this.jumper.localPosition != startPosition ) {
  69.             tempTime += Time.deltaTime;
  70.             float porcentagem = tempTime / ( totalTime * 0.5f ) ;
  71.             this.jumper.localPosition = Vector3.Lerp( halfwayPosition, startPosition,  porcentagem );
  72.             yield return null;
  73.         }
  74.     }
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top