Global Game Jam 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.0 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using NaughtyAttributes;
  5. /// <summary>
  6. /// Utility script to fake parent an object to another
  7. /// </summary>
  8. public class CopyTransform : MonoBehaviour
  9. {
  10. #region Inspector Fields
  11. [SerializeField, BoxGroup("References")]
  12. public Transform m_target;
  13. [SerializeField, Range(0, 1)]
  14. public float m_dampening = 1;
  15. #endregion Inspector Fields
  16. #region Private Fields
  17. private Vector3 m_positionOffset;
  18. private Quaternion m_rotationOffset;
  19. #endregion Private Fields
  20. #region Getters
  21. #endregion Getters
  22. #region MonoBehaviour Functions
  23. private void Awake()
  24. {
  25. m_positionOffset = transform.position - m_target.position;
  26. m_rotationOffset = m_target.rotation * Quaternion.Inverse(transform.rotation);
  27. }
  28. /// <summary>
  29. /// OnEnable is called when the object becomes enabled and active.
  30. /// </summary>
  31. private void OnEnable()
  32. {
  33. }
  34. /// <summary>
  35. /// OnDisable is called when the behaviour becomes disabled.
  36. /// </summary>
  37. private void OnDisable()
  38. {
  39. }
  40. /// <summary>
  41. /// Update is called once per frame
  42. /// </summary>
  43. private void FixedUpdate()
  44. {
  45. Vector3 expectedPosition = m_target.position + (m_target.rotation * m_positionOffset);
  46. Quaternion expectedRotation = m_target.rotation * m_rotationOffset;
  47. transform.position = Vector3.Lerp(transform.position, expectedPosition, m_dampening);
  48. transform.rotation = Quaternion.Lerp(transform.rotation, expectedRotation, m_dampening);
  49. }
  50. #endregion MonoBehaviour Functions
  51. #region Class Functionality
  52. #endregion Class Functionality
  53. #region Editor Functions
  54. /// <summary>
  55. /// Called when the Component is created or Reset from the Inspector
  56. /// </summary>
  57. private void Reset()
  58. {
  59. //useful for finding components on creation
  60. }
  61. #endregion Editor Functions
  62. }