Working gardener chase without flipping

This commit is contained in:
2025-09-19 15:51:50 +02:00
parent e1ee73bfb4
commit 6abf25b2ad
9 changed files with 2591 additions and 1234 deletions

View File

@@ -1,13 +1,15 @@
using UnityEngine;
using System.Security.Cryptography.X509Certificates;
using Pixelplacement;
using UnityEngine;
public class GardenerBehaviour : MonoBehaviour
{
private StateMachine stateMachineRef;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
stateMachineRef = GetComponent<StateMachine>();
}
// Update is called once per frame
@@ -15,4 +17,10 @@ public class GardenerBehaviour : MonoBehaviour
{
}
public void stateSwitch (string StateName)
{
Debug.Log("State Switch to: " + StateName);
stateMachineRef.ChangeState(StateName);
}
}

View File

@@ -7,63 +7,123 @@ public class LawnMowerChaseBehaviour : MonoBehaviour
public Transform LawnMowerObject;
public float chaseDuration;
public float chaseDelay;
[Range(0, 1)] public float startPercentage; // Exposed in Inspector
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
private const float AnchorThreshold = 0.05f;
private bool _wasAtStart = false;
private bool _wasAtEnd = false;
// For initial tween tracking
private bool _initialTweenActive = true;
private float _initialTargetAnchor = 1f;
void Start()
{
_originalScale = LawnMowerObject.localScale;
LawnMowerObject.position = ChaseSpline.GetPosition(startPercentage);
float distanceToStart = Mathf.Abs(startPercentage - 0f);
float distanceToEnd = Mathf.Abs(startPercentage - 1f);
if (distanceToStart < distanceToEnd)
{
// Tween from startPercentage to 1
_initialTargetAnchor = 1f;
Tween.Spline(
ChaseSpline,
LawnMowerObject,
startPercentage,
1,
false,
chaseDuration * (1 - startPercentage),
chaseDelay,
Tween.EaseInOut,
Tween.LoopType.None
);
}
else
{
// Tween from startPercentage to 0
_initialTargetAnchor = 0f;
Tween.Spline(
ChaseSpline,
LawnMowerObject,
startPercentage,
0,
false,
chaseDuration * startPercentage,
chaseDelay,
Tween.EaseInOut,
Tween.LoopType.None
);
}
_initialTweenActive = true;
}
void Update()
{
float percentage = ChaseSpline.ClosestPoint(LawnMowerObject.position);
// Handle initial tween completion
if (_initialTweenActive)
{
if (Mathf.Abs(percentage - _initialTargetAnchor) <= AnchorThreshold)
{
// Start ping-pong tween between extremes
StartPingPongTween(_initialTargetAnchor, 1f - _initialTargetAnchor);
_initialTweenActive = false;
}
return; // Don't process flip logic until ping-pong starts
}
// Detect start anchor
if (percentage <= AnchorThreshold)
{
if (!_wasAtStart)
{
flipSprite();
_wasAtStart = true;
_wasAtEnd = false;
}
}
// Detect end anchor
else if (percentage >= 1f - AnchorThreshold)
{
if (!_wasAtEnd)
{
flipSprite();
_wasAtEnd = true;
_wasAtStart = false;
}
}
else
{
_wasAtStart = false;
_wasAtEnd = false;
}
}
private void StartPingPongTween(float from, float to)
{
Tween.Spline(
ChaseSpline,
LawnMowerObject,
0,
1,
from,
to,
false,
chaseDuration,
chaseDelay,
0,
Tween.EaseInOut,
Tween.LoopType.PingPong,
onComplete: OnTweenComplete
Tween.LoopType.PingPong
);
}
private void OnTweenComplete()
private void flipSprite()
{
_movingForward = !_movingForward;
Flip(_movingForward);
Vector3 scale = LawnMowerObject.transform.localScale;
Vector3 rotation = LawnMowerObject.transform.eulerAngles;
scale.x *= -1;
rotation.z *= -1;
LawnMowerObject.transform.localScale = scale;
LawnMowerObject.transform.eulerAngles = rotation;
}
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() { }
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 1fda7fccaa5fbd04695f4c98d29bcbe0
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: