Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerMovement : MonoBehaviour {
- public float hSpeed; //horizontal walking speed
- public float sSpeed; //Running speed
- private float jSpeed; //Jump speed
- public float gravity; //Gravity
- private bool sprint = false;
- private bool isJumping = false;
- public Vector3 deltaMove;
- private CharacterController cc;
- // Use this for initialization
- void Start() {
- cc = GetComponent<CharacterController>();
- }
- // Update is called once per frame
- void Update() {
- MovePlayer();
- }
- void FixedUpdate() {
- }
- void MovePlayer() {
- sprint = Input.GetKey(KeyCode.JoystickButton0) || Input.GetKey(KeyCode.LeftShift); //Xbox A
- cc.Move(deltaMove);
- JumpPlayer();
- if (sprint == true) {
- deltaMove.x = Input.GetAxis("Horizontal") * sSpeed * Time.deltaTime; //sprinting
- } else {
- deltaMove.x = Input.GetAxis("Horizontal") * hSpeed * Time.deltaTime; //walking
- }
- cc.Move(deltaMove);
- }
- void JumpPlayer() {
- if (Input.GetKeyDown(KeyCode.JoystickButton1) || Input.GetKeyDown(KeyCode.Space) & cc.isGrounded) { //Xbox B
- isJumping = true;
- jSpeed = 15;
- Debug.Log("You Jumped!");
- } if (isJumping & jSpeed < 0) {
- deltaMove.y = (jSpeed - gravity) * Time.deltaTime;
- //jSpeed--;
- } if (Input.GetKeyUp(KeyCode.JoystickButton1) || Input.GetKeyUp(KeyCode.Space)) {
- isJumping = false;
- Debug.Log("You stopped jumping.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment