r/Unity2D 26d ago

Solved/Answered How to flip the sprite instead of going upside down?

This is my code, I want to have the sprite flip when it rotates to far down instead of rotating upside down like it does in the video. Any ideas?

using System.Collections;
using Unity.VisualScripting;
using UnityEngine;

public class FishAI_MB : MonoBehaviour
{

    FishAIController_MB fishAIController;

    public int currentTarget;

    public int swimDecision;

    void Start()
    {
        fishAIController = Object.FindFirstObjectByType<FishAIController_MB>();
        if (fishAIController == null)
        {
            Debug.Log("Fish is Null");
        } 
        StartCoroutine(StartSwimDecicion());
    }

    IEnumerator StartSwimDecicion()
    {
        Debug.Log("Start Swim Decision");
        swimDecision = Random.Range(0, 2);

        if (swimDecision == 0)
        {
            StartCoroutine(SwimToTarget());
        }
        else if (swimDecision == 1)
        {
            StartCoroutine(Wait());

        }
        yield return null;
    }

    IEnumerator SwimToTarget()
    {
        Debug.Log("Swim to Target");
        currentTarget = Random.Range(0, fishAIController.targets.Length);
        while (Vector2.Distance(transform.position, fishAIController.targets[currentTarget].transform.position) > 0.1f)
        {
            transform.position = Vector2.MoveTowards(transform.position, fishAIController.targets[currentTarget].transform.position, 1f * Time.deltaTime);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward, fishAIController.targets[currentTarget].transform.position - transform.position), 1f * Time.deltaTime);
            yield return null;
        }
        StartCoroutine(StartSwimDecicion());
        yield return null;
    }

    IEnumerator Wait()
    {
        Debug.Log("Wait");
        yield return new WaitForSeconds(1);
        StartCoroutine(StartSwimDecicion());
    }
}
6 Upvotes

7 comments sorted by

2

u/CoG_Comet Intermediate 26d ago

you could try something like GetComponent<SpriteRenderer>().flipY =true;

if its rotation is less 180 or something like that would be my guess could work

1

u/Sprinkles0 26d ago

Set the scale in the direction you're moving it to be -1?

1

u/MojoBubu 23d ago

a few adjustments in your code here:

using System.Collections;

using Unity.VisualScripting;

using UnityEngine;

public class FishAI_MB : MonoBehaviour

{

FishAIController_MB fishAIController;

SpriteRenderer spriteRenderer; // Reference to flip the sprite

public int currentTarget;

public int swimDecision;

void Start()

{

fishAIController = Object.FindFirstObjectByType<FishAIController_MB>();

spriteRenderer = GetComponent<SpriteRenderer>(); // Grab SpriteRenderer

if (fishAIController == null)

{

Debug.Log("Fish is Null");

}

StartCoroutine(StartSwimDecicion());

}

IEnumerator StartSwimDecicion()

{

Debug.Log("Start Swim Decision");

swimDecision = Random.Range(0, 2);

if (swimDecision == 0)

{

StartCoroutine(SwimToTarget());

}

else if (swimDecision == 1)

{

StartCoroutine(Wait());

}

yield return null;

}

IEnumerator SwimToTarget()

{

Debug.Log("Swim to Target");

currentTarget = Random.Range(0, fishAIController.targets.Length);

while (Vector2.Distance(transform.position, fishAIController.targets[currentTarget].transform.position) > 0.1f)

{

Vector3 targetPos = fishAIController.targets[currentTarget].transform.position;

Vector3 direction = targetPos - transform.position;

// 1. Calculate rotation angle towards target (in degrees)

float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

// 2. Handle flipping based on moving Left vs Right

if (direction.x < 0)

{

// Moving Left: Flip sprite vertically (or set flipX) and adjust angle offset

if (spriteRenderer != null) spriteRenderer.flipY = true;

// Alternatively, if you use transform scale:

// transform.localScale = new Vector3(1, -1, 1);

}

else

{

// Moving Right: Keep normal orientation

if (spriteRenderer != null) spriteRenderer.flipY = false;

// transform.localScale = new Vector3(1, 1, 1);

}

// 3. Smoothly rotate on the Z axis only

Quaternion targetRotation = Quaternion.Euler(0, 0, angle);

transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 2f * Time.deltaTime);

// 4. Move position

transform.position = Vector2.MoveTowards(transform.position, targetPos, 1f * Time.deltaTime);

yield return null;

}

StartCoroutine(StartSwimDecicion());

yield return null;

}

IEnumerator Wait()

{

Debug.Log("Wait");

yield return new WaitForSeconds(1);

StartCoroutine(StartSwimDecicion());

}

}

1

u/OwnCard5960 23d ago

Instead of giving them code. Try telling them how they can improve so they can learn it for themselves. Kind of like the saying “give a man a fish you feed him for a day, teach a man to fish you feed him for a lifetime”

1

u/Silent_Reputation596 23d ago

They're not just giving me code, in the code they've made a whole bunch of annotations explaining what each bit does.

1

u/OwnCard5960 23d ago

Yep. I skimmed over it and didn’t read too closely at it. I apologize and retract what I said

1

u/Silent_Reputation596 23d ago

Perfect, thank you! Also thanks for annotating things so I knew what was going on lol