83 lines
2.2 KiB
C#
83 lines
2.2 KiB
C#
using Pixelplacement;
|
|
using Pixelplacement.TweenSystem;
|
|
using UnityEngine;
|
|
|
|
public class soundBird_LandingBehaviour1 : MonoBehaviour
|
|
{
|
|
public Spline FlightSpline;
|
|
public Transform SoundBirdObject;
|
|
public float flightDuration;
|
|
public float flightDelay;
|
|
public soundBird_FlyingBehaviour flyingBehaviour;
|
|
private StateMachine stateMachine;
|
|
private Animator animator;
|
|
private TweenBase objectTween;
|
|
|
|
private float lastX;
|
|
private bool facingRight = true;
|
|
|
|
void Awake()
|
|
{
|
|
stateMachine = GetComponentInParent<StateMachine>();
|
|
animator = GetComponentInParent<Animator>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
Transform anchorB = transform.Find("AnchorB");
|
|
if (anchorB != null)
|
|
{
|
|
anchorB.position = flyingBehaviour.midFlightPosition;
|
|
}
|
|
objectTween = Tween.Spline(FlightSpline, SoundBirdObject, 0, 1, false, flightDuration, flightDelay, Tween.EaseOut, Tween.LoopType.None, HandleTweenStarted, HandleTweenFinished);
|
|
|
|
// Initialize lastX for flipping logic
|
|
if (SoundBirdObject != null)
|
|
{
|
|
lastX = SoundBirdObject.position.x;
|
|
}
|
|
}
|
|
|
|
void HandleTweenStarted()
|
|
{
|
|
|
|
}
|
|
void HandleTweenFinished()
|
|
{
|
|
if (SoundBirdObject != null)
|
|
{
|
|
objectTween.Cancel(); // Stop the spline tween for this object
|
|
}
|
|
//Logging.Debug("Tween finished!");
|
|
if (stateMachine != null)
|
|
{
|
|
animator.SetBool("isScared", false);
|
|
stateMachine.ChangeState("SoundBird"); // Change to the desired state name
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (SoundBirdObject == null) return;
|
|
|
|
float currentX = SoundBirdObject.position.x;
|
|
if (currentX > lastX && !facingRight)
|
|
{
|
|
Flip(true);
|
|
}
|
|
else if (currentX < lastX && facingRight)
|
|
{
|
|
Flip(false);
|
|
}
|
|
lastX = currentX;
|
|
}
|
|
|
|
private void Flip(bool faceRight)
|
|
{
|
|
Vector3 scale = SoundBirdObject.localScale;
|
|
scale.x = Mathf.Abs(scale.x) * (faceRight ? 1 : -1);
|
|
SoundBirdObject.localScale = scale;
|
|
facingRight = faceRight;
|
|
}
|
|
}
|