Advertisement
DugganSC

Untitled

Mar 15th, 2024
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using UnityEngine.XR.Interaction.Toolkit.AR;
  7.  
  8. public class ExaminableManager : MonoBehaviour
  9. {
  10.     [SerializeField]
  11.     Transform _examineTarget;
  12.  
  13.     Vector3 _priorPos;
  14.     Quaternion _priorRot;
  15.     Vector3 _priorScale;
  16.     Transform _priorParent;
  17.    
  18.     [SerializeField]
  19.     PlacementManager _placementManager;
  20.  
  21.     void Start()
  22.     {
  23.         if (_placementManager == null) {
  24.             _placementManager = GameObject.FindFirstObjectByType<PlacementManager>();
  25.  
  26.             if (_placementManager == null ) {
  27.                 Debug.LogError("#### Could not locate Placement Manager.");
  28.             }
  29.         }
  30.     }
  31.  
  32.     GameObject _examinedObject;
  33.     private bool isExamining = false;
  34.     private float _rotSpeed = 25f;
  35.  
  36.     private void Update()
  37.     {
  38.         if (isExamining && Input.touchCount > 0)
  39.         {
  40.             // Get first touch
  41.             Touch touch = Input.GetTouch(0);
  42.             if (touch.phase == TouchPhase.Moved)
  43.             {
  44.                 Vector3 rotation = new Vector3(touch.deltaPosition.x, touch.deltaPosition.y, 0) * _rotSpeed * Time.deltaTime;
  45.                 _examinedObject.transform.Rotate(rotation);
  46.             }
  47.         }
  48.     }
  49.  
  50.     public void ToggleExamination()
  51.     {
  52.         if (isExamining)
  53.         {
  54.             Unexamine();
  55.         } else
  56.         {
  57.             PerformExamination();
  58.         }
  59.     }
  60.  
  61.     public void PerformExamination()
  62.     {
  63.         GameObject selectedObject = _placementManager.GetSelectedObject();
  64.         if (selectedObject == null || _examineTarget == null)
  65.         {
  66.             Debug.Log($"Examine failed: Target: {selectedObject} examineTarget: {_examineTarget}");
  67.             return;
  68.         }
  69.  
  70.         Debug.Log($"Examining {selectedObject.gameObject.name}");
  71.  
  72.         if (selectedObject.TryGetComponent<Examinable>(out Examinable examinable))
  73.         {
  74.             _examinedObject = examinable.ExaminedObject;
  75.             _priorPos = _examinedObject.transform.localPosition;
  76.             _priorRot = _examinedObject.transform.localRotation;
  77.             _priorScale = _examinedObject.transform.localScale;
  78.             _priorParent = _examinedObject.transform.parent;
  79.  
  80.             _examinedObject.transform.position = _examineTarget.position;
  81.             _examinedObject.transform.parent = _examineTarget;
  82.             _examinedObject.transform.localScale = _priorScale * examinable.ExamineScaleOffset();
  83.         }
  84.  
  85.         isExamining = true;
  86.     }
  87.  
  88.     public void Unexamine()
  89.     {
  90.         _examinedObject.SetActive(true);
  91.         _examinedObject.transform.position = _priorPos;
  92.         _examinedObject.transform.rotation = _priorRot;
  93.         _examinedObject.transform.localScale = _priorScale;
  94.  
  95.         _examinedObject.transform.parent = _priorParent;
  96.  
  97.         isExamining = false;
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement