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.
 
 
 
 

87 lines
2.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;
/// <summary>
/// Utility script to fake parent an object to another
/// </summary>
public class CopyTransform : MonoBehaviour
{
#region Inspector Fields
[SerializeField, BoxGroup("References")]
public Transform m_target;
[SerializeField, Range(0, 1)]
public float m_dampening = 1;
#endregion Inspector Fields
#region Private Fields
private Vector3 m_positionOffset;
private Quaternion m_rotationOffset;
#endregion Private Fields
#region Getters
#endregion Getters
#region MonoBehaviour Functions
private void Awake()
{
m_positionOffset = transform.position - m_target.position;
m_rotationOffset = m_target.rotation * Quaternion.Inverse(transform.rotation);
}
/// <summary>
/// OnEnable is called when the object becomes enabled and active.
/// </summary>
private void OnEnable()
{
}
/// <summary>
/// OnDisable is called when the behaviour becomes disabled.
/// </summary>
private void OnDisable()
{
}
/// <summary>
/// Update is called once per frame
/// </summary>
private void FixedUpdate()
{
Vector3 expectedPosition = m_target.position + (m_target.rotation * m_positionOffset);
Quaternion expectedRotation = m_target.rotation * m_rotationOffset;
transform.position = Vector3.Lerp(transform.position, expectedPosition, m_dampening);
transform.rotation = Quaternion.Lerp(transform.rotation, expectedRotation, m_dampening);
}
#endregion MonoBehaviour Functions
#region Class Functionality
#endregion Class Functionality
#region Editor Functions
/// <summary>
/// Called when the Component is created or Reset from the Inspector
/// </summary>
private void Reset()
{
//useful for finding components on creation
}
#endregion Editor Functions
}