Skip to content

Instantly share code, notes, and snippets.

@IneonInoodle
Last active September 26, 2019 14:21
Show Gist options
  • Save IneonInoodle/beabfe28bd225dc2701a92605fe7b32d to your computer and use it in GitHub Desktop.
Save IneonInoodle/beabfe28bd225dc2701a92605fe7b32d to your computer and use it in GitHub Desktop.
WalkOnWalls
protected virtual void HandleStickyWallRotation()
{
// if our controller has hit any sticky platform. Normally then our .State.StickySlopeAngle will tell us where to rotate to
if (State.OnAStickyPlatform && !_shell.isInShell) //&& _jump.MyJumpState == JumpState.NotJumping
{
//Debug.Log("handle sticky wall rotation");
// if we were not grounded last frame that means we just hit sticky wall, set y velocity to zero to keep you b
// this would be the first frame we hit sticky wall
if (!State.WasStuckLastFrame)
{
if ((Vector2.Angle(Vector2.up, State.StickySlopeNormal) > 110))
{
if (_horizontalMovement.FlippedInput != true)
{
_horizontalMovement.FlippedInput = true;
_character.Flip();
}
}
else
{
_horizontalMovement.FlippedInput = false;
}
}
// get difference between our character angle and our desired angle
float _gravAngle = Vector2.Angle(State.StickySlopeNormal, transform.up);
//if (_gravAngle != 0) Debug.Log(_gravAngle);
//lerp to it for smooth results, fixing bug of weird jiggling. you need this as well so it insta lerps angels larger than 90. Without this you dont rotate fast enough to stick to a wall sometimes and you bounce off
if (_gravAngle < 30)
{
_rotateLerpTimer += Time.deltaTime / 4;
_gravAngle = Mathf.Lerp(0, _gravAngle, _rotateLerpTimer);
} else
{
//Debug.Log("flip " + _gravAngle);
// SetVerticalForce(0);
}
if (_rotateLerpTimer > 1)
_rotateLerpTimer = 0;
// the angles didnt flip correctly when character was walking left, only worked with right,
// this flips the angles so they work both ways
Vector3 _crossBelowSlopeAngle = Vector3.Cross(transform.up, State.StickySlopeNormal);
if (_crossBelowSlopeAngle.z < 0)
{
_gravAngle = -_gravAngle;
}
// get our current z rotation
float _overrideGravityAngle = transform.localEulerAngles.z;
// we add the desired difference to our angle to lerp outselves to have the correct rotation
_overrideGravityAngle += _gravAngle;
// convert the float we made back into a euler because you cant just do transform.localEulerAngles.z =
// this means you have to create a float then convert it back
_newRotationAngle.z = _overrideGravityAngle;
//rotate our character
transform.localEulerAngles = _newRotationAngle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment