Illia_R

Player Controller

Nov 20th, 2020 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. //Player Move
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     private CharacterController _controller;
  9.     private Vector2 _playerVelocity;
  10.     private bool _groundedPlayer;
  11.  
  12.     public float _gravityValue;
  13.     public float playerSpeed;
  14.     public float jumpHeight;
  15.  
  16.     private void Start()
  17.     {
  18.         _controller = GetComponent<CharacterController>();
  19.     }
  20.  
  21.     void FixedUpdate()
  22.     {
  23.         _groundedPlayer = _controller.isGrounded;
  24.  
  25.         if (_groundedPlayer && _playerVelocity.y < 0)
  26.         {
  27.             _playerVelocity.y = 0f;
  28.         }
  29.  
  30.         Vector2 move = new Vector2(Input.GetAxis("Horizontal"), 0);
  31.         _controller.Move(move * Time.fixedDeltaTime * playerSpeed);
  32.  
  33.         if (move != Vector2.zero)
  34.         {
  35.             gameObject.transform.forward = move;
  36.         }
  37.  
  38.         if (Input.GetKey(KeyCode.X))
  39.         {
  40.             _controller.Move(move * Time.fixedDeltaTime * playerSpeed * 1.5f);
  41.         }
  42.  
  43.        
  44.         if (Input.GetKey(KeyCode.Space) &&  _groundedPlayer)
  45.         {
  46.             _playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * _gravityValue);
  47.         }
  48.  
  49.         _playerVelocity.y += _gravityValue * Time.fixedDeltaTime;
  50.         _controller.Move(_playerVelocity * Time.fixedDeltaTime);
  51.     }
  52. }
  53.  
Add Comment
Please, Sign In to add comment