Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- private CharacterController _controller;
- private PlayerControls _playerInputs;
- [SerializeField]
- private float _moveSpeed = 5f;
- [SerializeField]
- private float _gravity = 10f;
- [SerializeField]
- private float _jumpHeight = 50f;
- private bool _isJumping;
- [SerializeField]
- private float _yVelocity;
- // Start is called before the first frame update
- void Start()
- {
- _controller = GetComponent<CharacterController>();
- _playerInputs = new PlayerControls();
- _playerInputs.Platforming.Enable();
- _playerInputs.Platforming.Jump.performed += Jump_performed;
- }
- private void Jump_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- if (_controller.isGrounded)
- {
- _isJumping = true;
- }
- }
- // Update is called once per frame
- void Update()
- {
- Vector2 playerMovement = _playerInputs.Platforming.Movement.ReadValue<Vector2>();
- playerMovement.Normalize();
- Vector3 velocity = new Vector3(playerMovement.x, 0, 0) * _moveSpeed;
- if (!_controller.isGrounded)
- {
- _yVelocity -= _gravity;
- }
- else
- {
- if (_isJumping)
- {
- _yVelocity = _jumpHeight;
- _isJumping = false;
- }
- else
- {
- _yVelocity = 0;
- }
- }
- velocity.y = _yVelocity;
- _controller.Move(velocity * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement