Shamba

cube movement(updated)

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