Shamba

Cube Movement(updated)

Feb 27th, 2018
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 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.     private 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.     // Use this for initialization
  20.     void Start () {
  21.     loopCount = 0;
  22.     onGround = true;
  23.     isAlive = true;
  24.     Started = false;
  25. }
  26.    
  27.     // Update is called once per frame
  28.     void Update () {
  29.         if(Input.GetMouseButtonDown(0))
  30.         {
  31.             StartText.gameObject.SetActive(false);
  32.             Started = true;
  33.         }
  34.         if (isAlive == true && Started == true)
  35.         {
  36.             onGround = isGrounded();
  37.             pos = Actor.transform.position;
  38.             Actor.transform.Translate(Vector3.forward * speed );
  39.             if (onGround == true)
  40.             {
  41.                 GameObject Actor2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  42.                 Actor2.transform.position = pos;
  43.                 Actor2.GetComponent<MeshRenderer>().material = mat;
  44.                 Actor2.GetComponent<BoxCollider>().isTrigger = true;
  45.                 if (Input.GetMouseButtonDown(0))
  46.                 {
  47.                     if (loopCount % 2 != 0)
  48.                     {
  49.                         Actor.transform.eulerAngles = new Vector3(0, 90, 0);
  50.                         loopCount++;
  51.                     }
  52.                     else
  53.                     {
  54.                         Actor.transform.eulerAngles = new Vector3(0, 0, 0);
  55.                         loopCount++;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     public bool isGrounded()
  62.     {
  63.         return Physics.Raycast(Actor.transform.position, Vector3.down, distFromGround);
  64.     }
  65.     private void OnCollisionEnter(Collision collision)
  66.     {
  67.         if (collision.gameObject.tag == "obstacle")
  68.         {
  69.             isAlive = false;
  70.             GameObject Prime =   GameObject.CreatePrimitive(PrimitiveType.Cube);
  71.             GameObject Prime1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  72.             GameObject Prime2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
  73.             Prime.transform.position = pos;
  74.             Prime1.transform.position = pos;
  75.             Prime2.transform.position = pos;
  76.             Prime.AddComponent<Rigidbody>().velocity=Random.onUnitSphere*50;
  77.             Prime1.AddComponent<Rigidbody>().velocity = Random.onUnitSphere * 50;
  78.             Prime2.AddComponent<Rigidbody>().velocity = Random.onUnitSphere * 100;
  79.             Prime.GetComponent<MeshRenderer>().material = mat;
  80.             Prime1.GetComponent<MeshRenderer>().material = mat;
  81.             Prime2.GetComponent<MeshRenderer>().material = mat;
  82.             RestartText.gameObject.SetActive(true);
  83.             Restartbutton.gameObject.SetActive(true);
  84.  
  85.  
  86.  
  87.         }
  88.     }
  89.     public void Restart()
  90.     {
  91.         SceneManager.LoadScene(0);
  92.     }
  93. }
Add Comment
Please, Sign In to add comment