Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class TokenBehaviour : MonoBehaviour
- {
- private Vector3 mOffset;
- private Vector3 startingPosition;
- private Vector3 slotPosition;
- private float mZCoord;
- private bool triggered = false;
- private Rigidbody rBody;
- private Collider TriggerVolume;
- private bool initTrigger = true;
- public Text countText;
- public Text winText;
- private float fDistance;
- private double dDistance;
- private Vector3 intersection;
- private Vector3[] TriggerVolumePositions = new Vector3[15];
- void Start()
- {
- rBody = GetComponent<Rigidbody>();
- SetCountText();
- winText.text = "";
- for (int i=0;i<15;i++)
- {
- TriggerVolumePositions[i] = GameObject.Find("TriggerVolume"+i.ToString()).transform.position;
- }
- }
- private void OnMouseDown()
- {
- startingPosition = transform.position;
- mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
- mOffset = gameObject.transform.position - GetMouseWorldPos();
- }
- private void OnMouseUp()
- {
- fDistance = Vector3.Distance(slotPosition, startingPosition);
- dDistance = Convert.ToDouble(fDistance);
- //Debug.Log(dDistance);
- if (triggered && (dDistance > 10.8 && dDistance < 13.4))
- {
- slotInBetween();
- //Debug.Log("SlotPos1 : " + slotPosition.ToString("F3"));
- transform.position = slotPosition;
- rBody.velocity = Vector3.zero;
- GameValue.numberOfPegsLeft--;
- GameObject.Find(TriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
- SetCountText();
- }
- else
- {
- transform.position = startingPosition;
- rBody.velocity = Vector3.zero;
- }
- }
- private Vector3 GetMouseWorldPos()
- {
- Vector3 mousePoint = Input.mousePosition;
- mousePoint.z = mZCoord;
- return Camera.main.ScreenToWorldPoint(mousePoint);
- }
- private void OnMouseDrag()
- {
- transform.position = GetMouseWorldPos() + mOffset;
- transform.position = new Vector3(transform.position.x, 6, transform.position.z);
- }
- void OnTriggerEnter(Collider other)//other.GetComponent<PegSlot>().object.name
- {
- if (other.gameObject.CompareTag("Trigger Volume") && GameObject.Find(other.gameObject.name).GetComponent<PegSlot>().freeSlot)
- {
- triggered = true;
- slotPosition = other.gameObject.transform.position;
- }
- }
- void OnTriggerExit(Collider other)
- {
- if(initTrigger && other.gameObject.CompareTag("Trigger Volume"))
- {
- TriggerVolume = other;
- initTrigger = false;
- //Debug.Log("Exited initTrigger");
- }
- else if (other.gameObject.CompareTag("Trigger Volume"))
- {
- triggered = false;
- }
- else if (other.gameObject.CompareTag("BoundingArea") && !GameValue.firstRoundDone)
- {
- GameValue.numberOfPegsLeft--;
- GameObject.Find(TriggerVolume.gameObject.name).GetComponent<PegSlot>().freeSlot = true;
- GameValue.firstRoundDone = true;
- gameObject.SetActive(false);
- SetCountText();
- }
- }
- void SetCountText()
- {
- countText.text = "Peg left : " + GameValue.numberOfPegsLeft.ToString();
- if (GameValue.numberOfPegsLeft == 0)
- {
- winText.text = "You win!";
- }
- }
- private string slotInBetween()
- {
- string slotInBetween = "";
- intersection.x = (startingPosition.x + slotPosition.x) /2;
- intersection.y = 4.54F;
- intersection.z = (startingPosition.z + slotPosition.z) /2;
- for (int i = 0; i < 15; i++)
- {
- if(((intersection.x > TriggerVolumePositions[i].x - 0.2) && (intersection.x < TriggerVolumePositions[i].x + 0.2)) && ((intersection.z > TriggerVolumePositions[i].z - 0.2) && (intersection.z < TriggerVolumePositions[i].z + 0.2)))
- {
- slotInBetween = "TriggerVolume" + i.ToString();
- }
- }
- return slotInBetween;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement