70 lines
1.8 KiB
C#
70 lines
1.8 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using Pixelplacement;
|
||
|
|
|
||
|
|
public class LawnMowerChaseBehaviour : MonoBehaviour
|
||
|
|
{
|
||
|
|
public Spline ChaseSpline;
|
||
|
|
public Transform LawnMowerObject;
|
||
|
|
public float chaseDuration;
|
||
|
|
public float chaseDelay;
|
||
|
|
|
||
|
|
private Vector3 _originalScale;
|
||
|
|
private bool _facingRight = true;
|
||
|
|
private bool _movingForward = true;
|
||
|
|
private float _lastPercentage = 0f;
|
||
|
|
private const float AnchorThreshold = 0.1f; // Tolerance for anchor detection
|
||
|
|
|
||
|
|
void Start()
|
||
|
|
{
|
||
|
|
_originalScale = LawnMowerObject.localScale;
|
||
|
|
|
||
|
|
Tween.Spline(
|
||
|
|
ChaseSpline,
|
||
|
|
LawnMowerObject,
|
||
|
|
0,
|
||
|
|
1,
|
||
|
|
false,
|
||
|
|
chaseDuration,
|
||
|
|
chaseDelay,
|
||
|
|
Tween.EaseInOut,
|
||
|
|
Tween.LoopType.PingPong,
|
||
|
|
onComplete: OnTweenComplete
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnTweenComplete()
|
||
|
|
{
|
||
|
|
_movingForward = !_movingForward;
|
||
|
|
Flip(_movingForward);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnTweenUpdate()
|
||
|
|
{
|
||
|
|
// Find the current percentage along the spline
|
||
|
|
float percentage = ChaseSpline.ClosestPoint(LawnMowerObject.position);
|
||
|
|
|
||
|
|
// Detect anchor arrival and flip accordingly
|
||
|
|
if (_facingRight && percentage >= 1f - AnchorThreshold)
|
||
|
|
{
|
||
|
|
Flip(false); // Face left at end anchor
|
||
|
|
_facingRight = false;
|
||
|
|
}
|
||
|
|
else if (!_facingRight && percentage <= AnchorThreshold)
|
||
|
|
{
|
||
|
|
Flip(true); // Face right at start anchor
|
||
|
|
_facingRight = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
_lastPercentage = percentage;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Flip(bool faceRight)
|
||
|
|
{
|
||
|
|
var scale = _originalScale;
|
||
|
|
scale.x = Mathf.Abs(scale.x) * (faceRight ? 1 : -1);
|
||
|
|
LawnMowerObject.localScale = scale;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update() { }
|
||
|
|
}
|