Bug Fix on Model Flip

Fixing the sprite flip on player and enemy models

While working on the hitbox animation for the Moss Giant, I noticed that all sprite models that require to flip on the X axis does so without the hitbox flipping as well.

The hitbox not flipping with the Moss Giant Model

This causes some bad interactions when the player is on the left side of the all enemy models. As I dwell deeper I also noticed that the Player model also suffers from this same problem. Since the hitbox is a special case with no sprite renderer, we are unable to use the Renderer FlipX function when the model needs to flip.

Fixing the Problem:

The logic behind the solution is quite simple, instead of flipping just the sprite when either the enemy is near the point destination or required to face the player, we can instead flip the entire parent object which houses both the sprite and hitbox.

Player Script:

Within our Player script we would need to replace our Sprite Renderer with the parent object which we will targeting the Transform component. We can call this new variable “_playerFlip”.

With the Sprite Renderer commented out, we will also need to comment out the methods that require the Sprite then we are going to use the Transform component and place a -1 on the X scale value. This negative value would cause the entire game object to flip including the sprite and hitbox.

The proper script would be using the local scale and giving the transform component a new Vector3 value where only the X axis changes to the negative 1.

Now you can see that the player flips according to the direction along with the hitbox. We can apply the same method for the Enemy Parent script.

Enemy Script:

In the Enemy script we are going to do the same thing by targeting the parent game object and it’s Transform component we can flip the entire model including the hitbox.

In the enemy script we are going to use “Protected” instead of “private”
In the Enemy Movement script when going to pointA to pointB

In within the facing player method, we can use the same method for flipping the model.

As you can see from the result on the bottom, we have successfully fix the issue, and all models including the player will flip accordingly.

--

--

--

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Staking is Multiplication!

Node.js mobile back-end on Azure App Services

NodeJS Deserialization Challenge created for YASCON-21

ReactJS for Noobs

functional Componnts-ReactJs

Welcome Walkthrough

Next.js SSR vs. SSG & Incrementalism

How to build a shopping cart using DragsterJS and JavaScript?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

More from Medium

How to Build an Interactive Security Camera System in Unity

How to Create A Loading Scene in Unity

Learn to code by making 2 D space shooter.

How To Use Post Processing In Unity