Advertisement
Guest User

Untitled

a guest
May 24th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class InfantryController : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private float _moveSpeed = 6.0f;
  10. [SerializeField]
  11. private float _jumpVelocity = 4f;
  12. [SerializeField]
  13. private float _isGroundedDelay = 0.215f;
  14.  
  15. private float _yVelocity = 0f;
  16. private float _isGroundedCountDown = 0f;
  17. private bool _wasGrounded;
  18.  
  19. private CharacterController _playerController;
  20.  
  21.  
  22. private void Start () {
  23. _playerController = GetComponent<CharacterController>();
  24. _wasGrounded = _playerController.isGrounded;
  25. _playerController.detectCollisions = true;
  26. }
  27.  
  28. private void Update () {
  29. isGrounded();
  30. move();
  31.  
  32. }
  33.  
  34. private void move () {
  35. Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
  36. Vector3 velocity = transform.TransformDirection(input).normalized * _moveSpeed;
  37.  
  38. if (_isGroundedCountDown >= 0 && Input.GetKey(KeyCode.Space)) {
  39. _yVelocity = _jumpVelocity * Input.GetAxisRaw("Jump");
  40. _isGroundedCountDown = -1f;
  41. }
  42. else {
  43. //if (_yVelocity < -10f)
  44. //_yVelocity += Physics.gravity.y * Time.deltaTime;
  45. //else
  46. _yVelocity += Physics.gravity.y / 20f;
  47. }
  48. velocity.y = _yVelocity;
  49.  
  50. _playerController.Move(velocity * Time.deltaTime);
  51. }
  52.  
  53. private void isGrounded () {
  54. if (!_playerController.isGrounded && _wasGrounded)
  55. _isGroundedCountDown = _isGroundedDelay;
  56. else if (!_playerController.isGrounded && !_wasGrounded)
  57. _isGroundedCountDown -= Time.deltaTime;
  58. else if (_playerController.isGrounded && !_wasGrounded)
  59. _isGroundedCountDown = _isGroundedDelay;
  60.  
  61. _wasGrounded = _playerController.isGrounded;
  62. }
  63.  
  64. //This is WIP for jump pads
  65. public void AddForce (float x, float y, float z, ForceMode mode) {
  66. _yVelocity = y;
  67. _isGroundedCountDown = -1f;
  68. }
  69.  
  70. private void FixedUpdate () {
  71.  
  72. }
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement