No edit summary Tag: Visual edit |
No edit summary Tag: Source edit |
||
Line 12: | Line 12: | ||
<br />using UnityEngine; |
<br />using UnityEngine; |
||
− | |||
using System.Collections; |
using System.Collections; |
||
− | |||
using AC; |
using AC; |
||
− | |||
public class SUC_FrameFlip : MonoBehaviour |
public class SUC_FrameFlip : MonoBehaviour |
||
− | |||
{ |
{ |
||
− | |||
[SerializeField] private AC.Char character; |
[SerializeField] private AC.Char character; |
||
− | |||
[SerializeField] private Transform transformToFlip; |
[SerializeField] private Transform transformToFlip; |
||
− | |||
[SerializeField] private AC_2DFrameFlipping frameFlipping; |
[SerializeField] private AC_2DFrameFlipping frameFlipping; |
||
− | |||
[SerializeField] private float topAngleFreedom = 0f; |
[SerializeField] private float topAngleFreedom = 0f; |
||
− | |||
private void Update () |
private void Update () |
||
− | |||
{ |
{ |
||
− | |||
if (frameFlipping == AC_2DFrameFlipping.None || character == null || transformToFlip == null) |
if (frameFlipping == AC_2DFrameFlipping.None || character == null || transformToFlip == null) |
||
− | |||
{ |
{ |
||
− | |||
return; |
return; |
||
− | |||
} |
} |
||
− | |||
bool doFlip = false; |
bool doFlip = false; |
||
− | |||
float spriteAngle = character.GetSpriteAngle (); |
float spriteAngle = character.GetSpriteAngle (); |
||
− | |||
switch (frameFlipping) |
switch (frameFlipping) |
||
− | |||
{ |
{ |
||
− | |||
case AC_2DFrameFlipping.LeftMirrorsRight: |
case AC_2DFrameFlipping.LeftMirrorsRight: |
||
− | |||
doFlip = (spriteAngle >= 0f && spriteAngle < 180f); |
doFlip = (spriteAngle >= 0f && spriteAngle < 180f); |
||
− | |||
break; |
break; |
||
− | |||
case AC_2DFrameFlipping.RightMirrorsLeft: |
case AC_2DFrameFlipping.RightMirrorsLeft: |
||
− | |||
doFlip = (spriteAngle > 180f && spriteAngle <= 360f); |
doFlip = (spriteAngle > 180f && spriteAngle <= 360f); |
||
− | |||
break; |
break; |
||
− | |||
default: |
default: |
||
− | |||
return; |
return; |
||
− | |||
} |
} |
||
− | |||
if (spriteAngle < topAngleFreedom || spriteAngle > (360f - topAngleFreedom) || (spriteAngle > (180f - topAngleFreedom) && spriteAngle < (180f + topAngleFreedom))) |
if (spriteAngle < topAngleFreedom || spriteAngle > (360f - topAngleFreedom) || (spriteAngle > (180f - topAngleFreedom) && spriteAngle < (180f + topAngleFreedom))) |
||
− | |||
{ |
{ |
||
− | |||
− | // Make no change |
||
− | |||
return; |
return; |
||
− | |||
} |
} |
||
− | |||
if ((doFlip && transformToFlip.localScale.x > 0f) || (!doFlip && transformToFlip.localScale.x < 0f)) |
if ((doFlip && transformToFlip.localScale.x > 0f) || (!doFlip && transformToFlip.localScale.x < 0f)) |
||
− | |||
{ |
{ |
||
− | |||
transformToFlip.localScale = new Vector3 (-transformToFlip.localScale.x, transformToFlip.localScale.y, transformToFlip.localScale.z); |
transformToFlip.localScale = new Vector3 (-transformToFlip.localScale.x, transformToFlip.localScale.y, transformToFlip.localScale.z); |
||
− | |||
} |
} |
||
− | |||
} |
} |
||
− | |||
− | |||
} |
} |
||
[[Category:General]] |
[[Category:General]] |
Latest revision as of 08:48, 17 January 2021
This script is used to automate the flipping of characters animated with the Sprites Unity Complex engine when facing a particular direction.
This is similar to the "Frame flipping" option when using the Sprites Unity engine, where left-facing animations can be flipped when facing right (and vice-versa).
To use:
- Setup your character using the Sprites Unity Complex engine as normal
- Update your Animator so that he plays the left-facing equivalent of any right-facing animation (or vice-versa) when appropriate.
- Place the code below in a C# script named SUC_FrameFlip
- Place the script on the character and assign the fields - setting the charater's Sprite child as the Transform To Flip.
- Tweak the Top Angle Freedom value to control how wide an angle a character can have when facing up or down before the script takes effect.
using UnityEngine; using System.Collections; using AC; public class SUC_FrameFlip : MonoBehaviour { [SerializeField] private AC.Char character; [SerializeField] private Transform transformToFlip; [SerializeField] private AC_2DFrameFlipping frameFlipping; [SerializeField] private float topAngleFreedom = 0f; private void Update () { if (frameFlipping == AC_2DFrameFlipping.None || character == null || transformToFlip == null) { return; } bool doFlip = false; float spriteAngle = character.GetSpriteAngle (); switch (frameFlipping) { case AC_2DFrameFlipping.LeftMirrorsRight: doFlip = (spriteAngle >= 0f && spriteAngle < 180f); break; case AC_2DFrameFlipping.RightMirrorsLeft: doFlip = (spriteAngle > 180f && spriteAngle <= 360f); break; default: return; } if (spriteAngle < topAngleFreedom || spriteAngle > (360f - topAngleFreedom) || (spriteAngle > (180f - topAngleFreedom) && spriteAngle < (180f + topAngleFreedom))) { return; } if ((doFlip && transformToFlip.localScale.x > 0f) || (!doFlip && transformToFlip.localScale.x < 0f)) { transformToFlip.localScale = new Vector3 (-transformToFlip.localScale.x, transformToFlip.localScale.y, transformToFlip.localScale.z); } } }