Last active
October 27, 2016 07:47
-
-
Save jarnik/1097a22642bbb0b3413abd076e528938 to your computer and use it in GitHub Desktop.
Spacr rotation alignment
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
Floor floor = World.player.floor; | |
if (floor != null) | |
{ | |
int stepTurn = 0; | |
if (Game.input.justPressed(GameInput.Action.WALK_TURN_LEFT)) | |
{ | |
stepTurn = -1; | |
} | |
if (Game.input.justPressed(GameInput.Action.WALK_TURN_RIGHT)) | |
{ | |
stepTurn = 1; | |
} | |
float playerCurrentAngle = this.transform.eulerAngles.z; | |
float targetAngle = floor.transform.eulerAngles.z; | |
targetAngle += Mathf.Round(playerCurrentAngle / 90f) * 90f; | |
if (stepTurn != 0) | |
{ | |
this.m_TargetAngleSet = true; | |
this.m_TargetAngle = targetAngle - 90f * stepTurn; | |
} | |
if (this.m_TargetAngleSet) | |
{ | |
targetAngle = this.m_TargetAngle; | |
} | |
float angleDiff = Mathf.DeltaAngle(targetAngle,playerCurrentAngle); | |
if ( this.m_TargetAngleSet && (Mathf.Abs(angleDiff) <= 0f)) | |
{ | |
this.m_TargetAngleSet = false; | |
} | |
Vector3 playerAngles = this.transform.eulerAngles; | |
if (Mathf.Abs(angleDiff) > 0f) | |
{ | |
if (Mathf.Abs(angleDiff) < 5f) | |
{ | |
playerCurrentAngle = targetAngle; | |
} | |
{ | |
playerAngles.z -= Mathf.Lerp(0, angleDiff,Time.deltaTime * 8.0f); | |
} | |
this.transform.eulerAngles = playerAngles; | |
} | |
} else | |
{ | |
this.m_TargetAngleSet = false; | |
int stepTurn = 0; | |
if (Game.input.down(GameInput.Action.WALK_TURN_LEFT)) | |
{ | |
stepTurn = -1; | |
} | |
if (Game.input.down(GameInput.Action.WALK_TURN_RIGHT)) | |
{ | |
stepTurn = 1; | |
} | |
if (stepTurn != 0) | |
{ | |
Vector3 playerAngles = this.transform.eulerAngles; | |
playerAngles.z += - stepTurn * Time.deltaTime * 90.0f; | |
this.transform.eulerAngles = playerAngles; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment