void SetCurrentDirection() { Tweak.Stats = ""; Tweak.Stats += "_animDirection: " + _animDirection.ToString() + "\r\n"; Tweak.Stats += "_movementDirection: " + _movementDirection.ToString() + "\r\n"; if (_movementDirection != Vector2.Zero) { // Get direction we are pushing AnimationDirection newDirection = DirectionHelper.GetDirectionFromHeading(_movementDirection); if (newDirection == _animDirection) { return; } if (newDirection == DirectionHelper.GetOppositeDirection(_animDirection)) { // If pushing in the opposite direction to current facing, change instantly _animDirection = newDirection; PlayNewCharacterAnimation(); } else { // Only change when our heading when velocity starts moving slightly towards our heading Vector2 velocity = DynamicBody.LinearVelocity; if (velocity.LengthSquared() < 0.01f) { return; } Vector2 normalHeading = Vector2.Normalize(_movementDirection); Vector2 normalVelocity = Vector2.Normalize(velocity); float directionDotProduct = Vector2.Dot(normalHeading, normalVelocity); if (directionDotProduct > DIRECTION_CHANGE_THRESHOLD) { _animDirection = newDirection; PlayNewCharacterAnimation(); } } } }
void UpdateDirectionBasedOnVelocity() { if (_heading.LengthSquared() > 0.001f) { // Get direction enemy is moving AnimationDirection newDirection = DirectionHelper.GetDirectionFromHeadingBiasHorizontal(_heading, 0.2f); if (newDirection == _animDirection) { return; } if (newDirection == DirectionHelper.GetOppositeDirection(_animDirection)) { // If moving in the opposite direction to current facing, change instantly _animDirection = newDirection; PlayNewEnemyAnimation(); } else { // Only change when our heading when velocity starts moving slightly towards our heading Vector2 velocity = DynamicBody.LinearVelocity; if (velocity.LengthSquared() < 0.1f) { return; } // Avoid flickering Vector2 normalHeading = Vector2.Normalize(_heading); Vector2 normalVelocity = Vector2.Normalize(velocity); float directionDotProduct = Vector2.Dot(normalHeading, normalVelocity); if (directionDotProduct > DIRECTION_CHANGE_THRESHOLD) { _animDirection = newDirection; PlayNewEnemyAnimation(); } } } }