Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TransformMenuMovement : MonoBehaviour {
- Bounds toolBounds;
- GameObject cursor;
- bool initialSetupComplete = true;
- void OnEnable () {
- NRSRManager.ObjectFocused += Enabled_GetPosition;
- NRSRManager.ObjectUnFocused += Disabled_Reset;
- toolBounds = GetBoundsForAllChildren(gameObject);
- cursor = GameObject.Find("Cursor");
- }
- private void OnDisable()
- {
- NRSRManager.ObjectFocused -= Enabled_GetPosition;
- NRSRManager.ObjectUnFocused -= Disabled_Reset;
- }
- void Disabled_Reset()
- {
- initialSetupComplete = true;
- }
- void Enabled_GetPosition()
- {
- if (initialSetupComplete)
- {
- transform.position = cursor.transform.position;
- transform.rotation = cursor.transform.rotation * Quaternion.Euler(0, 0, 180);
- initialSetupComplete = false;
- }
- }
- void Update () {
- if(transform.localPosition.x - cursor.transform.localPosition.x > toolBounds.extents.x / 3 ||
- transform.localPosition.x - cursor.transform.localPosition.x < -toolBounds.extents.x / 3 ||
- transform.localPosition.y - cursor.transform.localPosition.y > toolBounds.extents.y /3 ||
- transform.localPosition.y - cursor.transform.localPosition.y < - toolBounds.extents.y /3)
- {
- transform.position = Vector3.Lerp(transform.position, cursor.transform.position, 0.02f);
- }
- transform.rotation = Quaternion.Lerp(transform.rotation,
- cursor.transform.rotation * Quaternion.Euler(0, 0, 180),
- 1);
- transform.position = new Vector3(transform.position.x,
- transform.position.y,
- cursor.transform.position.z - 0.1f);
- }
- public Bounds GetBoundsForAllChildren(GameObject findMyBounds)
- {
- Bounds result = new Bounds(Vector3.zero, Vector3.zero);
- foreach (Collider coll in findMyBounds.GetComponentsInChildren<Collider>())
- {
- if (result.extents == Vector3.zero)
- {
- result = coll.bounds;
- }
- else
- {
- result.Encapsulate(coll.bounds);
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement