Skip to content

Instantly share code, notes, and snippets.

@muzudho
Last active January 21, 2017 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muzudho/8b3ef0f3810150b7caaa09121e432826 to your computer and use it in GitHub Desktop.
Save muzudho/8b3ef0f3810150b7caaa09121e432826 to your computer and use it in GitHub Desktop.
Unityで2D格闘(2D Fighting game)作るときに相手の方向を向くには? ref: http://qiita.com/muzudho1/items/cfe352c864d791c3a98f
相手 - 自分
/// <summary>
/// 相手に向かっていっているかどうか。めんどうなので、相手に重なっている、Equal は無しで。
/// </summary>
public enum FacingOpponentFwBk
{
Forward,
Back
}
/// <summary>
/// 相手は左か右か。めんどうなので、相手に重なっている、Equal は無しで。
/// </summary>
public enum FacingOpponentLR
{
Left,
Right
}
FacingOpponentFwBk GetFacingOpponentFwBk(float leverX)
{
if (Mathf.Sign(SceneCommon.Player_to_transform[(int)CommonScript.ReverseTeban((PlayerIndex)playerIndex)].position.x - transform.position.x)
==
Mathf.Sign(leverX)
)
{
return FacingOpponentFwBk.Forward;
}
return FacingOpponentFwBk.Back;
}
FacingOpponentLR GetFacingOfOpponentLR()
{
// 自分と相手の位置(相手が右側にいるとき正となるようにする)
if( 0<=SceneCommon.Player_to_transform[(int)CommonScript.ReverseTeban((PlayerIndex)playerIndex)].position.x - transform.position.x)
{
return FacingOpponentLR.Right;
}
return FacingOpponentLR.Left;
}
void DoFacingOpponent(FacingOpponentLR facingOpponentLR)
{
//localScale.xを-1にすると画像が反転する
Vector2 temp = transform.localScale;
switch (facingOpponentLR)
{
case FacingOpponentLR.Left: // 左を向くぜ☆
temp.x = -1 * SceneCommon.GRAPHIC_SCALE;
break;
case FacingOpponentLR.Right: // 右を向くぜ☆
temp.x = 1 * SceneCommon.GRAPHIC_SCALE;
break;
default:
break;
}
transform.localScale = temp;
}
if (FacingOpponentFwBk.Forward == facingOpponentFwBk)
{
// 相手に向かっていくぜ☆
}
else
{
// 相手から離れていくぜ☆
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment