Mizraim

CharacterMovement

Sep 28th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7.     public float hSpeed; //horizontal walking speed
  8.     public float sSpeed; //Running speed
  9.     private float jSpeed; //Jump speed
  10.     public float gravity; //Gravity
  11.     private bool sprint = false;
  12.     private bool isJumping = false;
  13.     public Vector3 deltaMove;
  14.     private CharacterController cc;
  15.  
  16.     // Use this for initialization
  17.     void Start() {
  18.         cc = GetComponent<CharacterController>();
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update() {
  23.         MovePlayer();
  24.     }
  25.     void FixedUpdate() {
  26.     }
  27.     void MovePlayer() {
  28.         sprint = Input.GetKey(KeyCode.JoystickButton0) || Input.GetKey(KeyCode.LeftShift); //Xbox A
  29.         cc.Move(deltaMove);
  30.         JumpPlayer();
  31.         if (sprint == true) {
  32.             deltaMove.x = Input.GetAxis("Horizontal") * sSpeed * Time.deltaTime; //sprinting
  33.         } else {
  34.             deltaMove.x = Input.GetAxis("Horizontal") * hSpeed * Time.deltaTime; //walking
  35.         }
  36.         cc.Move(deltaMove);
  37.     }
  38.     void JumpPlayer() {
  39.         if (Input.GetKeyDown(KeyCode.JoystickButton1) || Input.GetKeyDown(KeyCode.Space) & cc.isGrounded) { //Xbox B
  40.             isJumping = true;
  41.             jSpeed = 15;
  42.             Debug.Log("You Jumped!");
  43.         } if (isJumping & jSpeed < 0) {
  44.             deltaMove.y = (jSpeed - gravity) * Time.deltaTime;
  45.             //jSpeed--;
  46.         } if (Input.GetKeyUp(KeyCode.JoystickButton1) || Input.GetKeyUp(KeyCode.Space)) {
  47.             isJumping = false;
  48.             Debug.Log("You stopped jumping.");
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment