Shamba

Cube Movement(updated)

Mar 5th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class CubeMovement : MonoBehaviour {
  7.     public GameObject Actor;
  8.     public float speed = 1;
  9.     private Vector3 pos;
  10.     public Material mat;
  11.     public static int loopCount = 0;
  12.     public bool onGround = true;
  13.     public float distFromGround = 0.6f;
  14.     public bool isAlive = true;
  15.     public GameObject StartText;
  16.     public GameObject RestartText;
  17.     public GameObject Restartbutton;
  18.     public bool Started = false;
  19.     public GameObject music;
  20.     public static bool uturn = false;
  21.     // Use this for initialization
  22.     void Start ()
  23.     {
  24.        loopCount = 0;
  25.        onGround = true;
  26.        isAlive = true;
  27.        Started = false;
  28.     }
  29.    
  30.     // Update is called once per frame
  31.     void Update () {
  32.         if(Input.GetMouseButtonDown(0))
  33.         {
  34.             StartText.gameObject.SetActive(false);
  35.             Started = true;
  36.             music.gameObject.SetActive(true);
  37.         }
  38.         if (isAlive == true && Started == true)
  39.         {
  40.          
  41.             onGround = isGrounded();
  42.             pos = Actor.transform.position;
  43.             Actor.transform.Translate(Vector3.forward * speed );
  44.             if (onGround == true)
  45.             {
  46.                 GameObject Actor2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  47.                 Actor2.transform.position = pos;
  48.                 Actor2.GetComponent<MeshRenderer>().material = mat;
  49.                 Actor2.GetComponent<BoxCollider>().isTrigger = true;
  50.                 if (Input.GetMouseButtonDown(0))
  51.                 {
  52.                     if (uturn == false)
  53.                     {
  54.                         if (loopCount % 2 != 0)
  55.                         {
  56.                             Actor.transform.eulerAngles = new Vector3(0, 90, 0);
  57.                             loopCount++;
  58.                         }
  59.                         else
  60.                         {
  61.                             Actor.transform.eulerAngles = new Vector3(0, 0, 0);
  62.                             loopCount++;
  63.                         }
  64.                     }
  65.                     if(uturn == true)
  66.                     {
  67.                         if (loopCount % 2 != 0)
  68.                         {
  69.                             Actor.transform.eulerAngles = new Vector3(0, 90, 0);
  70.                             loopCount++;
  71.                         }
  72.                         else
  73.                         {
  74.                             Actor.transform.eulerAngles = new Vector3(0, 180, 0);
  75.                             loopCount++;
  76.                         }
  77.                     }
  78.                    
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     public bool isGrounded()
  84.     {
  85.         return Physics.Raycast(Actor.transform.position, Vector3.down, distFromGround);
  86.     }
  87.     private void OnCollisionEnter(Collision collision)
  88.     {
  89.         if (collision.gameObject.tag == "obstacle")
  90.         {
  91.             isAlive = false;
  92.             Started = false;
  93.             music.gameObject.SetActive(false);
  94.             GameObject Prime =   GameObject.CreatePrimitive(PrimitiveType.Cube);
  95.             GameObject Prime1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  96.             GameObject Prime2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  97.             Prime.transform.position = pos;
  98.             Prime1.transform.position = pos;
  99.             Prime2.transform.position = pos;
  100.             Prime.AddComponent<Rigidbody>().velocity=Random.onUnitSphere*50;
  101.             Prime1.AddComponent<Rigidbody>().velocity = Random.onUnitSphere * 50;
  102.             Prime2.AddComponent<Rigidbody>().velocity = Random.onUnitSphere * 100;
  103.             Prime.GetComponent<MeshRenderer>().material = mat;
  104.             Prime1.GetComponent<MeshRenderer>().material = mat;
  105.             Prime2.GetComponent<MeshRenderer>().material = mat;
  106.             RestartText.gameObject.SetActive(true);
  107.             Restartbutton.gameObject.SetActive(true);
  108.  
  109.  
  110.  
  111.         }
  112.        
  113.     }
  114.     public void Restart()
  115.     {
  116.         SceneManager.LoadScene(0);
  117.     }
  118. }
Add Comment
Please, Sign In to add comment