Advertisement
VelhoRabugento

Teste14

Nov 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FishMoveAquarium : MonoBehaviour {
  6.  
  7.     [Header("Amplitude das rotacoes em torno de cada eixo")]
  8.     [Header("Eixo Z")]
  9.     [SerializeField] float maximoAnguloZ;
  10.     [SerializeField] float minimoAnguloZ;
  11.  
  12.     [Header("Velocidades de rotacao")]
  13.     [SerializeField] float velocidadeDeRotacaoZ;
  14.  
  15.     [Header("em torno de quais eixos deve rodar?")]
  16.     [SerializeField] bool z;
  17.  
  18.     bool rodarZPositivamente;
  19.     bool movimentar;
  20.     bool colidiu;
  21.  
  22.     float rotZ;
  23.  
  24.     public float speed;
  25.  
  26.     private float distance;
  27.  
  28.     private Rigidbody rb;
  29.  
  30.  
  31.     // Use this for initialization
  32.     void Start() {
  33.  
  34.         rb = GetComponent<Rigidbody>();
  35.         colidiu = false;
  36.         movimentar = true;
  37.         rotZ = transform.rotation.eulerAngles.z;
  38.     }
  39.  
  40.     // Update is called once per frame
  41.     void Update() {
  42.  
  43.         if (colidiu)
  44.         {
  45.             Colidiu();
  46.         }
  47.         else
  48.         {
  49.             NaoColidiu();
  50.         }
  51.  
  52.     }
  53.  
  54.     void OnTriggerEnter(Collider other)
  55.     {
  56.         if (other.gameObject.tag == "wallRight" || other.gameObject.tag == "wallLeft")
  57.         {
  58.             colidiu = true;
  59.             AplicaRotacoes();
  60.         }
  61.     }
  62.  
  63.     void OnTriggerExit(Collider other)
  64.     {
  65.         colidiu = false;
  66.         NaoColidiu();
  67.     }
  68.  
  69.     void AplicaRotacoes()
  70.     {
  71.         if (rotZ < minimoAnguloZ)
  72.         {
  73.             rodarZPositivamente = true;
  74.         }
  75.         if (rotZ > maximoAnguloZ)
  76.         {
  77.             rodarZPositivamente = false;
  78.         }
  79.  
  80.         if (z)
  81.         {
  82.             if (rodarZPositivamente)
  83.             {
  84.                 movimentar = false;
  85.                 transform.Rotate(0, 0, velocidadeDeRotacaoZ * Time.deltaTime);
  86.                 rotZ += Time.deltaTime * velocidadeDeRotacaoZ;
  87.             }
  88.             else
  89.             {
  90.                 movimentar = false;
  91.                 transform.Rotate(0, 0, -velocidadeDeRotacaoZ * Time.deltaTime);
  92.                 rotZ -= Time.deltaTime * velocidadeDeRotacaoZ;
  93.             }
  94.  
  95.             if (!rodarZPositivamente)
  96.             {
  97.                 movimentar = true;
  98.                 transform.Translate(0, speed * Time.deltaTime, 0);
  99.             }
  100.             else
  101.             {
  102.                 movimentar = true;
  103.                 transform.Translate(0, -speed * Time.deltaTime, 0);
  104.             }
  105.         }
  106.     }
  107.    
  108.    
  109.    
  110.     void Colidiu()
  111.     {
  112.         if (colidiu)
  113.         {
  114.             AplicaRotacoes();
  115.         }
  116.         else
  117.         {
  118.             NaoColidiu();
  119.         }
  120.     }
  121.  
  122.     void NaoColidiu()
  123.     {
  124.         if (!colidiu)
  125.         {
  126.             transform.Translate(0, speed * Time.deltaTime, 0);
  127.         }
  128.         else
  129.         {
  130.             transform.Translate(0, -speed * Time.deltaTime, 0);
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement