using UnityEngine; using Pixelplacement; using Pixelplacement.TweenSystem; public class GardenerChaseBehavior : MonoBehaviour { public Spline ChaseSpline; public Transform GardenerObject; public float chaseDuration; public float chaseDelay; [SerializeField] private Animator animator; [SerializeField] public GameObject lawnMowerRef; private TweenBase tweenRef; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { tweenRef = Tween.Spline (ChaseSpline, GardenerObject, 0, 1, false, chaseDuration, chaseDelay, Tween.EaseLinear, Tween.LoopType.None, HandleTweenStarted, HandleTweenFinished); } void HandleTweenFinished () { //Debug.Log ("Tween finished!"); tweenRef.Stop(); Destroy(ChaseSpline); var gardenerSpriteRef = gameObject.transform.Find("GardenerRunningSprite"); //ReparentWithExactWorldTransform(gardenerSpriteRef, lawnMowerRef.transform); gardenerSpriteRef.transform.SetParent(lawnMowerRef.transform, true); //Vector3 pos = gardenerSpriteRef.position; //pos.y = lawnMowerRef.transform.position.y; //gardenerSpriteRef.position = pos; } void HandleTweenStarted () { //Debug.Log ("Tween started!"); animator.SetBool("IsIdle?", false); } void Awake() { } // Update is called once per frame void Update() { } public void ReparentWithExactWorldTransform(Transform objectToMove, Transform newParent) { // Store the original world position, rotation, and scale Vector3 originalWorldPosition = objectToMove.position; Quaternion originalWorldRotation = objectToMove.rotation; Vector3 originalWorldScale = objectToMove.lossyScale; // First change the parent objectToMove.SetParent(newParent, false); // Set worldPositionStays to false // Then manually restore world position and rotation objectToMove.position = originalWorldPosition; objectToMove.rotation = originalWorldRotation; // Correct the scale (this is tricky because localScale isn't the same as world scale) if (newParent != null) { Vector3 newLocalScale = objectToMove.localScale; Vector3 parentWorldScale = newParent.lossyScale; // Adjust local scale to maintain world scale newLocalScale.x = (Mathf.Approximately(parentWorldScale.x, 0f)) ? 0f : originalWorldScale.x / parentWorldScale.x; newLocalScale.y = (Mathf.Approximately(parentWorldScale.y, 0f)) ? 0f : originalWorldScale.y / parentWorldScale.y; newLocalScale.z = (Mathf.Approximately(parentWorldScale.z, 0f)) ? 0f : originalWorldScale.z / parentWorldScale.z; objectToMove.localScale = newLocalScale; } } }