Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class PlayerController : MonoBehaviour
- {
- //Variables
- public float speed = 5.0F;
- public float jumpSpeed = 8.0F;
- public float gravity = 20.0F;
- private Vector3 moveDirection = Vector3.zero;
- bool hasSkateboard = true;
- void Update()
- {
- CharacterController controller = GetComponent<CharacterController>();
- // is the controller on the ground?
- if (controller.isGrounded)
- {
- //Feed moveDirection with input.
- moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- //Prevents player from moving twice as fast by holding two keys.
- if (moveDirection.sqrMagnitude > 1.0f)
- {
- moveDirection.Normalize();
- }
- moveDirection = transform.TransformDirection(moveDirection);
- //Multiply it by speed.
- moveDirection *= speed;
- //Jumping
- if (Input.GetButton("Jump"))
- moveDirection.y = jumpSpeed;
- //Is shift held down, and does the player have a skateboard? If so, increase speed (Using skateboard)
- if (hasSkateboard == true && Input.GetKey(KeyCode.LeftShift))
- {
- speed = 10.0F;
- }
- }
- else
- { //If the player has fallen off the platform, return them to the platform.
- if (transform.position.y < -7)
- {
- transform.position = new Vector3(0, 2.7f, 0);
- }
- }
- //If escape is pressed, quit the game.
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- Application.Quit();
- }
- //No skateboard? Fine, walking speed then (5.0f, not extremely slow, but infuriating)
- else
- {
- speed = 5.0F;
- }
- //Applying gravity to the controller
- moveDirection.y -= gravity * Time.deltaTime;
- //Making the character move
- controller.Move(moveDirection * Time.deltaTime);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement