Advertisement
Combreal

pegGame02w.cs

Mar 28th, 2020
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class TokenBehaviour : MonoBehaviour
  7. {
  8.     private Vector3 mOffset;
  9.     private Vector3 startingPosition;
  10.     private Vector3 slotPosition;
  11.     private float mZCoord;
  12.     private bool triggered = false;
  13.     private Rigidbody rBody;
  14.     private string pegSlotName;
  15.  
  16.     void Start()
  17.     {
  18.         rBody = GetComponent<Rigidbody>();
  19.     }
  20.  
  21.     private void OnMouseDown()
  22.     {
  23.         startingPosition = transform.position;
  24.         mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
  25.         mOffset = gameObject.transform.position - GetMouseWorldPos();
  26.     }
  27.  
  28.     private void OnMouseUp()
  29.     {
  30.         if (triggered)
  31.         {
  32.             transform.position = slotPosition;
  33.             rBody.velocity = Vector3.zero;
  34.         }
  35.         else
  36.         {
  37.             transform.position = startingPosition;
  38.             rBody.velocity = Vector3.zero;
  39.         }
  40.     }
  41.  
  42.     private Vector3 GetMouseWorldPos()
  43.     {
  44.         Vector3 mousePoint = Input.mousePosition;
  45.         mousePoint.z = mZCoord;
  46.         return Camera.main.ScreenToWorldPoint(mousePoint);
  47.     }
  48.  
  49.     private void OnMouseDrag()
  50.     {
  51.         transform.position = GetMouseWorldPos() + mOffset;
  52.         transform.position = new Vector3(transform.position.x, 6, transform.position.z);
  53.     }
  54.  
  55.     void OnTriggerEnter(Collider other)
  56.     {
  57.         if (other.gameObject.CompareTag("Trigger Volume"))
  58.         {
  59.             pegSlotName = (other.gameObject.name).ToString();
  60.             triggered = true;
  61.             slotPosition = other.gameObject.transform.position;
  62.         }
  63.     }
  64.  
  65.     void OnTriggerExit(Collider other)
  66.     {
  67.         if (other.gameObject.CompareTag("Trigger Volume"))
  68.         {
  69.             triggered = false;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement