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;
- void Start()
- {
- rBody = GetComponent<Rigidbody>();
- SetCountText();
- winText.text = "";
- }
- private void OnMouseDown()
- {
- startingPosition = transform.position;
- mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
- mOffset = gameObject.transform.position - GetMouseWorldPos();
- }
- private void OnMouseUp()
- {
- if (triggered)
- {
- //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 = "Count : " + GameValue.numberOfPegsLeft.ToString();
- if (GameValue.numberOfPegsLeft == 0)
- {
- winText.text = "You win!";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement