Last active
January 21, 2017 04:08
-
-
Save muzudho/8b3ef0f3810150b7caaa09121e432826 to your computer and use it in GitHub Desktop.
Unityで2D格闘(2D Fighting game)作るときに相手の方向を向くには? ref: http://qiita.com/muzudho1/items/cfe352c864d791c3a98f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
相手 - 自分 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 相手に向かっていっているかどうか。めんどうなので、相手に重なっている、Equal は無しで。 | |
/// </summary> | |
public enum FacingOpponentFwBk | |
{ | |
Forward, | |
Back | |
} | |
/// <summary> | |
/// 相手は左か右か。めんどうなので、相手に重なっている、Equal は無しで。 | |
/// </summary> | |
public enum FacingOpponentLR | |
{ | |
Left, | |
Right | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (FacingOpponentFwBk.Forward == facingOpponentFwBk) | |
{ | |
// 相手に向かっていくぜ☆ | |
} | |
else | |
{ | |
// 相手から離れていくぜ☆ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment