Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class InfantryController : MonoBehaviour
- {
- [SerializeField]
- private float _moveSpeed = 6.0f;
- [SerializeField]
- private float _jumpVelocity = 4f;
- [SerializeField]
- private float _isGroundedDelay = 0.215f;
- private float _yVelocity = 0f;
- private float _isGroundedCountDown = 0f;
- private bool _wasGrounded;
- private CharacterController _playerController;
- private void Start () {
- _playerController = GetComponent<CharacterController>();
- _wasGrounded = _playerController.isGrounded;
- _playerController.detectCollisions = true;
- }
- private void Update () {
- isGrounded();
- move();
- }
- private void move () {
- Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
- Vector3 velocity = transform.TransformDirection(input).normalized * _moveSpeed;
- if (_isGroundedCountDown >= 0 && Input.GetKey(KeyCode.Space)) {
- _yVelocity = _jumpVelocity * Input.GetAxisRaw("Jump");
- _isGroundedCountDown = -1f;
- }
- else {
- //if (_yVelocity < -10f)
- //_yVelocity += Physics.gravity.y * Time.deltaTime;
- //else
- _yVelocity += Physics.gravity.y / 20f;
- }
- velocity.y = _yVelocity;
- _playerController.Move(velocity * Time.deltaTime);
- }
- private void isGrounded () {
- if (!_playerController.isGrounded && _wasGrounded)
- _isGroundedCountDown = _isGroundedDelay;
- else if (!_playerController.isGrounded && !_wasGrounded)
- _isGroundedCountDown -= Time.deltaTime;
- else if (_playerController.isGrounded && !_wasGrounded)
- _isGroundedCountDown = _isGroundedDelay;
- _wasGrounded = _playerController.isGrounded;
- }
- //This is WIP for jump pads
- public void AddForce (float x, float y, float z, ForceMode mode) {
- _yVelocity = y;
- _isGroundedCountDown = -1f;
- }
- private void FixedUpdate () {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement