Skip to content
AI & Software Engineering
AI & Software Engineering

Explore AI, AI Agents, software engineering, and the technologies shaping the future.

  • Home
  • AI & Agents
  • System Architecture
  • Project Management
  • Projects
  • Tutorials
  • Tech History
AI & Software Engineering

Explore AI, AI Agents, software engineering, and the technologies shaping the future.

Unity in Practice 0010 – Flipping a 2D Character Horizontally in Unity

WCSee, May 8, 2025May 17, 2025

🛠 Problem:

The character doesn’t animate correctly or looks wrong when moving left in previous tutorial: Unity in Practice 0009 – How to Use Sprite Sheets in Unity for 2D Animation and UI – Wonderful Code See

🎯 Solution:

To fix the left run animation that appears incorrect, you generally don’t need a separate animation for left movement. Instead, use sprite flipping to mirror the existing run animation.

To flip the character in Unity when moving left or right, you can Flips the GameObject on the Y-axis to visually mirror it. Here’s how to do it:

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

public class BallScript : MonoBehaviour
{

    private Rigidbody2D rb;
    private Animator playerAnim;

    [SerializeField] private float moveSpeed = 5;
    [SerializeField] private float jumpForce = 10;

    private float xInput;
    private bool isRun;
    private int facingDirection = 1;
    private bool facingRight = true;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        playerAnim = GetComponentInChildren<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        // Get horizontal input
        xInput = Input.GetAxis("Horizontal");

        // Apply horizontal movement
        rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);


        // Jump on Space key press
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }

        // Flip the player based on input direction
        FlipController();

        // Animate player running
        isRun = rb.velocity.x != 0;
        playerAnim.SetBool("IsRun", isRun);
    }

    private void Flip()
    {
        facingDirection = facingDirection * -1;
        facingRight = !facingRight;

        transform.Rotate(0, 180, 0);
    }

    private void FlipController()
    {
        if (rb.velocity.x > 0 && !facingRight)
        {
            Flip();
        }
        else if (rb.velocity.x < 0 && facingRight)
        {
            Flip();
        }
    }
}
Please follow and like us:
RSS
Facebook
Facebook
fb-share-icon
X (Twitter)
Visit Us
Follow Me
Tweet
Pinterest
Pinterest
fb-share-icon
Post Views: 285

Related posts:

Unity in Practice 0012 – Blend Tree in Unity Unity in Practice 0014 – Unity 2D Dash and Dash Cooldown with Time.deltaTime Unity in Practice 0011 – Player Jump with Ground Check in Unity Unity in Practice 0009 – How to Use Sprite Sheets in Unity for 2D Animation and UI Unity in Practice 0007 – Very First Unity C# Code to Move and Jump a 2D Ball Unity in Practice 0008 – Encapsulation and Inspector Access with Unity’s [SerializeField] Unity Game Development – Systematic Learning Roadmap (2025 Edition) Unity in Practice 0006 – Very first C# code to demonstrate how to capture input
Tech Notes Flip CharacterGame Development

Post navigation

Previous post
Next post

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Chapter 5 — Major AI Agent Architecture Patterns
  • Chapter 4 — What Is an Agent Harness?
  • Chapter 3 — The Agent Loop: The Core Execution Cycle of an AI Agent
  • AI Agent Architecture and Engineering Practice Guide
  • Chapter 2 — LLM Application Architecture: Core Components and Engineering Foundations
  • Chapter 1: Rethinking AI Agents
  • The Key Milestones in the History of Artificial Intelligence (2026.08)
  • Building a Python-based AI Agent with LangGraph and OpenRouter: A Hands-On Guide
  • System Architecture Design: What Is It Really “Designing”? Understanding Through a Building Analogy
  • When Data Exceeds Memory: Choosing Between Pandas, Dask, and DuckDB for Efficient Analytics

Recent Comments

  • WCSee on Chapter 1: Rethinking AI Agents
  • WCSee on Chapter 2 — LLM Application Architecture: Core Components and Engineering Foundations
  • WCSee on AI Agent Architecture and Engineering Practice Guide
©2026 AI & Software Engineering | WordPress Theme by SuperbThemes
Manage Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}