Advertisement
Combreal

pegGame01.cs

Mar 27th, 2020
685
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 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.  
  15.     void Start()
  16.     {
  17.         rBody = GetComponent<Rigidbody>();
  18.     }
  19.  
  20.     private void OnMouseDown()
  21.     {
  22.         startingPosition = transform.position;
  23.         transform.position = transform.position + new Vector3(0, 2, 0);
  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.             //Debug.Log("Entered volume");
  33.             Debug.Log(slotPosition.ToString("F3"));
  34.             //transform.position = slotPosition;
  35.             rBody.MovePosition(slotPosition);
  36.             rBody.velocity = Vector3.zero;
  37.         }
  38.         else
  39.         {
  40.             transform.position = startingPosition;
  41.             //rBody.MovePosition(startingPosition);
  42.             rBody.velocity = Vector3.zero;
  43.         }
  44.         //rBody.detectCollisions = true;
  45.         //rBody.isKinematic = false;
  46.     }
  47.  
  48.     private Vector3 GetMouseWorldPos()
  49.     {
  50.         Vector3 mousePoint = Input.mousePosition;
  51.         mousePoint.z = mZCoord;
  52.         return Camera.main.ScreenToWorldPoint(mousePoint);
  53.     }
  54.  
  55.     private void OnMouseDrag()
  56.     {
  57.         transform.position = GetMouseWorldPos() + mOffset;
  58.         transform.position = new Vector3(transform.position.x, 4, transform.position.z);
  59.     }
  60.  
  61.     void OnTriggerEnter(Collider other)
  62.     {
  63.         if (other.gameObject.CompareTag("Trigger Volume"))
  64.         {
  65.             triggered = true;
  66.             slotPosition = other.gameObject.transform.position;
  67.             //slotPosition.z = -2.69F;
  68.         }
  69.         //rBody.detectCollisions = false;
  70.         //rBody.isKinematic = true;
  71.     }
  72.  
  73.     void OnTriggerExit(Collider other)
  74.     {
  75.         if (other.gameObject.CompareTag("Trigger Volume"))
  76.         {
  77.             triggered = false;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement