Created
September 4, 2022 08:59
-
-
Save Wolfos/c2d495d79ce1e4485f80295d31c8c7e3 to your computer and use it in GitHub Desktop.
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
using FlaxEngine; | |
namespace Game | |
{ | |
/// <summary> | |
/// PlayerMovement Script. | |
/// </summary> | |
public class PlayerMovement : Script | |
{ | |
[Serialize, ShowInEditor] private float _maxWalkSpeed; | |
[Serialize, ShowInEditor] private float _accelerationTime; | |
[Serialize, ShowInEditor] private float _decelerationTime; | |
[Serialize, ShowInEditor] private float _upGravity = 0.8f; | |
[Serialize, ShowInEditor] private float _downGravity = 1.0f; | |
[Serialize, ShowInEditor] private float _jumpVelocity = 100; | |
[Serialize, ShowInEditor] private float _minVelocity = 2f; | |
[Serialize, ShowInEditor] private float _airControl = 0.5f; | |
[Space(20)] | |
[Serialize, ShowInEditor] private CharacterController _characterController; | |
[Serialize, ShowInEditor] private Actor _playerCamera; | |
[Serialize, ShowInEditor] private Actor _graphic; | |
[Serialize, ShowInEditor] private AnimatedModel _animator; | |
private AnimGraphParameter _walkSpeedParam; | |
private AnimGraphParameter _jumpParam; | |
private AnimGraphParameter _groundedParam; | |
private float _verticalVelocity; | |
private Vector3 _velocity; | |
private Vector2 _input; | |
/// <inheritdoc/> | |
public override void OnAwake() | |
{ | |
_walkSpeedParam = _animator.GetParameter("WalkSpeed"); | |
_jumpParam = _animator.GetParameter("Jump"); | |
_groundedParam = _animator.GetParameter("Grounded"); | |
} | |
public override void OnUpdate() | |
{ | |
_input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); | |
} | |
/// <inheritdoc/> | |
public override void OnFixedUpdate() | |
{ | |
Movement(); | |
Rotation(); | |
} | |
private void Movement() | |
{ | |
var deltaTime = 1 / Time.PhysicsFPS; | |
// Velocity | |
var forward = _playerCamera.Transform.Forward; | |
var right = _playerCamera.Transform.Right; | |
forward *= _input.Y; | |
right *= _input.X; | |
float control = 1; | |
if (!_characterController.IsGrounded) | |
{ | |
control = _airControl; | |
} | |
// Accelerate | |
_velocity += (forward + right) * _maxWalkSpeed / _accelerationTime * deltaTime * control; | |
// Decelerate | |
if (_input.LengthSquared < 0.1f && _velocity.LengthSquared > _minVelocity * _minVelocity) | |
{ | |
var deceleration = _velocity.Normalized * _maxWalkSpeed / _decelerationTime * deltaTime; | |
_velocity -= deceleration; | |
} | |
_velocity = Vector3.ClampLength(_velocity, _maxWalkSpeed); | |
if (_velocity.LengthSquared < _minVelocity * _minVelocity && | |
_input.LengthSquared < 0.1f) | |
{ | |
_velocity = Vector3.Zero; | |
} | |
// Animation | |
var walkAnimationSpeed = _velocity.Length * 0.1f; | |
_walkSpeedParam.Value = walkAnimationSpeed; | |
_groundedParam.Value = _characterController.IsGrounded; | |
// Vertical velocity | |
if (_characterController.IsGrounded && _verticalVelocity < 0) | |
{ | |
_verticalVelocity = 0; | |
_jumpParam.Value = false; | |
} | |
if (_verticalVelocity > 0) | |
{ | |
_verticalVelocity -= _upGravity * deltaTime * 100; | |
} | |
else | |
{ | |
_verticalVelocity -= _downGravity * deltaTime * 100; | |
} | |
if (_characterController.IsGrounded && Input.GetKeyDown(KeyboardKeys.Spacebar)) | |
{ | |
_verticalVelocity = _jumpVelocity; | |
_jumpParam.Value = true; | |
} | |
// Move | |
_characterController.Move(_velocity * deltaTime * 60 + _verticalVelocity * Vector3.Up); | |
} | |
private void Rotation() | |
{ | |
if (_velocity.LengthSquared > 0.1f) | |
{ | |
var angle = Mathf.Atan2(_velocity.X, _velocity.Z) * Mathf.RadiansToDegrees; | |
var rotation = Matrix.RotationQuaternion(Quaternion.Euler(new Vector3(0, angle, 0))); | |
_graphic.Rotation = rotation; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment