Free Roaming AI Facing Direction It's Moving

Hello,
I apologize if this has already been posted but I cannot find a tutorial or any solution on here for my problem. I am making an Aquarium Sim and I want the fish to swim freely and face the direction they are swimming in. I don’t want my fish swimming backward.

This is my code rn:

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

public class Swim : MonoBehaviour
{
    public float speed;
    private float waitTime;
    public float startWaitTime;
    private float input;

    public Transform patrolPoint;
    public float minX;
    public float maxX;
    public float minY;
    public float maxY;



    // Start is called before the first frame update
    void Start()
    {
        waitTime = startWaitTime;

        patrolPoint.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
    }

    // Update is called once per frame
    void Update()
    {

        transform.position = Vector2.MoveTowards(transform.position, patrolPoint.position, speed * Time.deltaTime);

        if (Vector2.Distance(transform.position, patrolPoint.position) < 0.1f)
            if (waitTime <= 0)
            {
                patrolPoint.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
                waitTime = startWaitTime;
            }
            else
            {
                waitTime -= Time.deltaTime;
            }

    }

    void Flip()
    {
        transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
        speed *= -1;
    }

}

Everything I have tried does not seem to phase the sprite in any way. Can you help me?

Screenshots of code are not a thing. If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

In order to reason about your problem, you must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Apologizes, this was the easiest way to share it at the time. Here is the code in it’s proper form.

Excellent. Now find out what the directions and flippings are in real time.

The first thing I notice is you are independently multiplying speed by -1 and scale. Why not just have a boolean that says “facing right” and if so, move right and face right. If not move left and face left. No multiplying multiple different uncorrelated states, hoping they stay in sync, etc.

I found a solution that works. It’s not perfect but it does its job well enough.
Any advice on improving it, I would appreciate it. I’m still learning. :slight_smile:

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

public class Swim : MonoBehaviour
{
    public float speed;
    private float waitTime;
    public float startWaitTime;
    private float input;

    public Transform patrolPoint;
    public float minX;
    public float maxX;
    public float minY;
    public float maxY;

    public bool facingRight;
    public float horizontalValue;



    // Start is called before the first frame update
    void Start()
    {
        waitTime = startWaitTime;

        patrolPoint.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
    }

    // Update is called once per frame
    void Update()
    {

        transform.position = Vector2.MoveTowards(transform.position, patrolPoint.position, speed * Time.deltaTime);
        {

            if (Vector2.Distance(transform.position, patrolPoint.position) <= 0.1f)

                if (patrolPoint.transform.position.x > gameObject.transform.position.x && facingRight)
                    Flip();
            if (patrolPoint.transform.position.x < gameObject.transform.position.x && !facingRight)
                Flip();

            if (waitTime <= 0)
            {
                patrolPoint.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
                waitTime = startWaitTime;
            }
            else
            {
                waitTime -= Time.deltaTime;
            }

        }
    }

        void Flip()
        {
            //here your flip funktion, as example
            facingRight = !facingRight;
            transform.Rotate(new Vector3(0, 180, 0));
        }


    }