Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Baller : MonoBehaviour {
  6.     public GameObject leftHandAnchor;
  7.     public GameObject rightHandAnchor;
  8.     public GameObject ballPrefab;
  9.     public GameObject haloPrefab;
  10.     public GameObject cylinderPrefab;
  11.     public float threshold = 0.03f;
  12.     public float r = 0.1f;
  13.     // For keeping track
  14.     private List<GameObject> BList = new List<GameObject>();
  15.     private List<GameObject> HList = new List<GameObject>();
  16.     private List<GameObject> CList = new List<GameObject>();
  17.     private List<List<GameObject>> VertexList = new List<List<GameObject>>();
  18.     private List<List<GameObject>> EdgeList = new List<List<GameObject>>();
  19.     private List<GameObject> TempEdgeList1 = new List<GameObject>();
  20.     private List<GameObject> TempEdgeList2 = new List<GameObject>();
  21.     private int vertex;
  22.     private int edge;
  23.     private int radius;
  24.     // For adding
  25.     private GameObject clone;
  26.     private GameObject halo;
  27.     private GameObject cylinder;
  28.     static int num;
  29.     // For deleting
  30.     private GameObject nearBall;
  31.     private GameObject nearHalo;
  32.     private Rigidbody rb;
  33.    
  34.     // Update is called once per frame
  35.     void Update () {
  36.         // Mask to not detect hands when deleting objects in scene
  37.         int layerMask = 1 << 2;
  38.         layerMask = ~layerMask;
  39.        
  40.         // When to add balls and halos
  41.         if (OVRInput.GetDown(OVRInput.RawButton.B)) {
  42.             if (rightHandAnchor != null) {
  43.                 AddBall(rightHandAnchor);
  44.                 UpdateRadii();
  45.             }
  46.         }
  47.         if (OVRInput.GetDown(OVRInput.RawButton.Y)) {
  48.             if (leftHandAnchor != null) {
  49.                 AddBall(leftHandAnchor);
  50.                 UpdateRadii();
  51.             }
  52.         }
  53.        
  54.         // When to remove balls and halos
  55.         if (OVRInput.GetDown(OVRInput.RawButton.A)) {
  56.             if (rightHandAnchor != null) {
  57.                 RemoveBall(rightHandAnchor);
  58.             }
  59.         }
  60.         if (OVRInput.GetDown(OVRInput.RawButton.X)) {
  61.             if (leftHandAnchor != null) {
  62.                 RemoveBall(leftHandAnchor);
  63.             }
  64.         }
  65.        
  66.         // When to change radius of halos
  67.         if(OVRInput.Get(OVRInput.Button.PrimaryThumbstickUp)) {
  68.             r += 0.02f;
  69.             UpdateRadii();
  70.         }
  71.        
  72.         if(OVRInput.Get(OVRInput.Button.PrimaryThumbstickDown)) {
  73.             r -= 0.02f;
  74.             UpdateRadii();
  75.         }
  76.        
  77.         // When to adjust connecting cylinders
  78.         foreach(GameObject ball in BList){
  79.             if(ball.transform.hasChanged){
  80.                 Debug.Log("Whoa!");
  81.                 int vertex = BList.IndexOf(ball);
  82.                 foreach(GameObject cylinder in EdgeList[vertex]){
  83.                     int edge = CList.IndexOf(cylinder);
  84.                     cylinder.transform.position = (VertexList[edge][0].transform.position + VertexList[edge][1].transform.position)/2;
  85.                     cylinder.transform.rotation = Quaternion.FromToRotation(Vector3.up, VertexList[edge][0].transform.position - VertexList[edge][1].transform.position);
  86.                     cylinder.transform.localScale = new Vector3(0.015f, Vector3.Distance(VertexList[edge][0].transform.position/2, VertexList[edge][1].transform.position/2), 0.015f);
  87.                 }
  88.                 ball.transform.hasChanged = false;
  89.             }
  90.         }
  91.     }
  92.    
  93.     // Adds a ball and a halo at the input anchor position
  94.     void AddBall (GameObject anchor) {
  95.         // Instantiate ball at anchor position
  96.         GameObject clone = (GameObject)Instantiate(ballPrefab, anchor.transform.position, anchor.transform.rotation);
  97.         clone.name = "clone" + num.ToString();
  98.         TempEdgeList1.Clear();
  99.         // Instantiate cylinder between anchor and other clones
  100.         foreach (GameObject existing_ball in BList){
  101.             GameObject cylinder = (GameObject)Instantiate(cylinderPrefab, (anchor.transform.position + existing_ball.transform.position)/2, Quaternion.FromToRotation(Vector3.up, anchor.transform.position - existing_ball.transform.position));
  102.             cylinder.transform.localScale = new Vector3(0.015f, Vector3.Distance(existing_ball.transform.position/2, anchor.transform.position/2), 0.015f);
  103.             int vertex = BList.IndexOf(existing_ball);
  104.             cylinder.name = "cylinder-" + num.ToString() + "-" + vertex.ToString();
  105.             // Add cylinder to lists
  106.             CList.Add(cylinder);
  107.             TempEdgeList1.Add(cylinder);
  108.             //EdgeList[vertex].Add(cylinder);
  109.             //Debug.Log("Ball " + vertex + " now has " + EdgeList[vertex].Count + " neighbors");
  110.             // Add vertices of cylinder to list
  111.             VertexList.Add(new List<GameObject> { clone, existing_ball });
  112.         }
  113.         // Add ball to list
  114.         BList.Add(clone);
  115.         // Add edges of ball to list
  116.         EdgeList.Add(TempEdgeList1);
  117.         // Instantiate halo at anchor position, as child of ball
  118.         GameObject halo = (GameObject)Instantiate(haloPrefab, anchor.transform.position, anchor.transform.rotation);
  119.         halo.name = "halo" + num.ToString();
  120.         halo.transform.SetParent(clone.transform);
  121.         HList.Add(halo);
  122.         // Increase counter
  123.         num++;
  124.     }
  125.  
  126.     // Removes all balls and halos within a threshold distance of the anchor position
  127.     void RemoveBall (GameObject anchor) {
  128.         // Find objects within threshold of anchor
  129.         RaycastHit[] hits = Physics.SphereCastAll(anchor.transform.position, threshold, anchor.transform.forward, 0f);
  130.         foreach (RaycastHit ball in hits){
  131.             Debug.Log("GONNA DESTROY: " + ball.collider.name);
  132.             nearBall = ball.collider.gameObject;
  133.             nearHalo = nearBall.transform.GetChild(0).gameObject;
  134.             // Remove from lists
  135.             BList.Remove(nearBall);
  136.             HList.Remove(nearHalo);
  137.             // Drop ball to bottom, make invisible
  138.             rb = nearBall.GetComponent<Rigidbody>();
  139.             rb.isKinematic = false;
  140.             rb.useGravity = true;
  141.             Destroy(nearBall.GetComponent<MeshRenderer>());
  142.             Destroy(nearHalo.GetComponent<MeshRenderer>());
  143.         }
  144.     }
  145.  
  146.     void UpdateRadii () {
  147.         foreach (GameObject halo in HList) {
  148.             halo.transform.localScale = new Vector3(r, r, r);
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement