1
\$\begingroup\$

I have a waypoint script I use to control enemy patrol movement in a given area. I use a reference to the Sprite Renderer to change the sprite facing direction. It starts with flipX set to false, by default, then is supposed to flip the sprite to the right direction, based on which trigger collider it detects. I have attached flipped if and else if statement below.

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("Waypoint2"))
    {
        sr.flipX = true;
    }
    else if (collision.gameObject.CompareTag("Waypoint"))
    {
        sr.flipX = false;
    }
}

However, when I press play to playtest, the sprite starts in the correct direction (walking toward Waypoint 1) but does not flip to face WP2. It then walks backward to WP2, and flips, which then causes him to walk backward toward WP1. I have switched the sr.flipX values to the opposite of what they are now, and have even moved the waypoints in the actual world.

Below is code showing waypoint index to create custom waypoints for each individual entity to have its own set.

[SerializeField] private GameObject[] waypoints;
private int currentWaypointIndex = 0;
\$\endgroup\$
2
  • \$\begingroup\$ Why are you sharing code as an image? \$\endgroup\$
    – user35344
    Oct 14, 2021 at 19:55
  • \$\begingroup\$ Well I guess I didn't realize it was frowned upon. Updated to show code in proper format. \$\endgroup\$
    – TBG
    Oct 14, 2021 at 19:57

1 Answer 1

1
\$\begingroup\$

Well, since you asked for advice, I'm going to suggest this whole method of hardcoding the flip direction based on the gameObject's tag isn't going to be very easy or fun to maintain over time as you add/ move around waypoints, etc.

Instead, I suggest you simply calculate whether the next waypoint is in front or behind the sprite using the dot product and flip accordingly.

DEMO

demo

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Sprite))]
public class WaypointMarch : MonoBehaviour
{
    [SerializeField] Transform[] waypoints;
    [SerializeField] AnimationCurve curve; // one way of moving our character around
    public int currentWaypointIdx; //exposed for debugging
    public Transform oldWayPoint, nextWayPoint;
    float timer;
    SpriteRenderer sprite;

    void Start()
    {
        sprite = GetComponent<SpriteRenderer>();
        oldWayPoint = waypoints[1];
        nextWayPoint = waypoints[0];
        currentWaypointIdx = 1;
    }


    void Update()
    {
        timer += Time.deltaTime;
        transform.position = Vector3.Lerp(oldWayPoint.position, nextWayPoint.position, curve.Evaluate(timer));
        if (timer >= 1.0f)
        {
            UpdateWaypoints();
        }
    }

    void UpdateWaypoints()
    {
        currentWaypointIdx = currentWaypointIdx >= waypoints.Length - 1 ? 0 : currentWaypointIdx + 1;
        oldWayPoint = nextWayPoint;
        nextWayPoint = waypoints[currentWaypointIdx];
        if (Vector3.Dot(-transform.right, transform.position - nextWayPoint.position) < 0)
        {
            Debug.Log("behind");
            sprite.flipX = true;
        }
        else
        {
            Debug.Log("ahead");
            sprite.flipX = false;
        }
        timer = 0;
    }
}

Code should be pretty straightforward to follow; to move the sprite from one WP to another I'm using an animation curve; when the sprite reaches the target waypoint (timer = 1) we increment or rollover the waypoint index; old WP becomes the current next WP and next WP is the next one in the array. We check to see if the next WP is ahead of us (to the right, in this case) and flip the sprite accordingly.

Scene Hierarchy:

Scene Hierarchy

Character Settings: Character Settings

\$\endgroup\$
4
  • \$\begingroup\$ Thank you, I will look into that. I just added code showing my waypoint index, as each individual entity has its own set of waypoints to follow. So, with that being said, would the dot product still be the better method then? \$\endgroup\$
    – TBG
    Oct 14, 2021 at 19:41
  • \$\begingroup\$ well, yes, because you wouldn't have to configure anything (assuming all your sprites face the same way by default) \$\endgroup\$ Oct 14, 2021 at 19:49
  • \$\begingroup\$ Ok, thank you again. \$\endgroup\$
    – TBG
    Oct 14, 2021 at 19:56
  • \$\begingroup\$ You're welcome. Updated answer with demo script; hopefully that will get you going! \$\endgroup\$ Oct 14, 2021 at 21:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .