Advertisement
Combreal

pegGame03w.cs

Mar 28th, 2020
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class TokenBehaviour : MonoBehaviour
  8. {
  9.     private Vector3 mOffset;
  10.     private Vector3 startingPosition;
  11.     private Vector3 slotPosition;
  12.     private float mZCoord;
  13.     private bool triggered = false;
  14.     private Rigidbody rBody;
  15.     private Collider TriggerVolume;
  16.     private bool initTrigger = true;
  17.     public Text countText;
  18.     public Text winText;
  19.  
  20.     void Start()
  21.     {
  22.         rBody = GetComponent<Rigidbody>();
  23.         SetCountText();
  24.         winText.text = "";
  25.     }
  26.  
  27.     private void OnMouseDown()
  28.     {
  29.         startingPosition = transform.position;
  30.         mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
  31.         mOffset = gameObject.transform.position - GetMouseWorldPos();
  32.     }
  33.  
  34.     private void OnMouseUp()
  35.     {
  36.         if (triggered)
  37.         {
  38.             //Debug.Log("SlotPos1 : " + slotPosition.ToString("F3"));
  39.             transform.position = slotPosition;
  40.             rBody.velocity = Vector3.zero;
  41.             GameValue.numberOfPegsLeft--;
  42.             GameObject.Find(TriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  43.             SetCountText();
  44.         }
  45.         else
  46.         {
  47.             transform.position = startingPosition;
  48.             rBody.velocity = Vector3.zero;
  49.         }
  50.     }
  51.  
  52.     private Vector3 GetMouseWorldPos()
  53.     {
  54.         Vector3 mousePoint = Input.mousePosition;
  55.         mousePoint.z = mZCoord;
  56.         return Camera.main.ScreenToWorldPoint(mousePoint);
  57.     }
  58.  
  59.     private void OnMouseDrag()
  60.     {
  61.         transform.position = GetMouseWorldPos() + mOffset;
  62.         transform.position = new Vector3(transform.position.x, 6, transform.position.z);
  63.     }
  64.  
  65.     void OnTriggerEnter(Collider other)//other.GetComponent<PegSlot>().object.name
  66.     {
  67.         if (other.gameObject.CompareTag("Trigger Volume") && GameObject.Find(other.gameObject.name).GetComponent<PegSlot>().freeSlot)
  68.         {
  69.             triggered = true;
  70.             slotPosition = other.gameObject.transform.position;
  71.         }
  72.     }
  73.  
  74.     void OnTriggerExit(Collider other)
  75.     {
  76.         if(initTrigger && other.gameObject.CompareTag("Trigger Volume"))
  77.         {
  78.             TriggerVolume = other;
  79.             initTrigger = false;
  80.             //Debug.Log("Exited initTrigger");
  81.         }
  82.         else if (other.gameObject.CompareTag("Trigger Volume"))
  83.         {
  84.             triggered = false;
  85.         }
  86.         else if (other.gameObject.CompareTag("BoundingArea") && !GameValue.firstRoundDone)
  87.         {
  88.             GameValue.numberOfPegsLeft--;
  89.             GameObject.Find(TriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
  90.             GameValue.firstRoundDone = true;
  91.             gameObject.SetActive(false);
  92.             SetCountText();
  93.         }
  94.     }
  95.  
  96.     void SetCountText()
  97.     {
  98.         countText.text = "Count : " + GameValue.numberOfPegsLeft.ToString();
  99.         if (GameValue.numberOfPegsLeft == 0)
  100.         {
  101.             winText.text = "You win!";
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement