Advertisement
Guest User

Life RPG Movement

a guest
Nov 24th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.     //Variables
  7.     public float speed = 5.0F;
  8.     public float jumpSpeed = 8.0F;
  9.     public float gravity = 20.0F;
  10.     private Vector3 moveDirection = Vector3.zero;
  11.     bool hasSkateboard = true;
  12.    
  13.     void Update()
  14.     {
  15.         CharacterController controller = GetComponent<CharacterController>();
  16.         // is the controller on the ground?
  17.         if (controller.isGrounded)
  18.         {
  19.             //Feed moveDirection with input.
  20.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  21.  
  22.             //Prevents player from moving twice as fast by holding two keys.
  23.             if (moveDirection.sqrMagnitude > 1.0f)
  24.             {
  25.                 moveDirection.Normalize();
  26.             }
  27.  
  28.             moveDirection = transform.TransformDirection(moveDirection);
  29.             //Multiply it by speed.
  30.             moveDirection *= speed;
  31.             //Jumping
  32.             if (Input.GetButton("Jump"))
  33.                 moveDirection.y = jumpSpeed;
  34.  
  35.             //Is shift held down, and does the player have a skateboard? If so, increase speed (Using skateboard)
  36.             if (hasSkateboard == true && Input.GetKey(KeyCode.LeftShift))
  37.             {
  38.                 speed = 10.0F;
  39.             }
  40.  
  41.         }
  42.  
  43.         else
  44.         {   //If the player has fallen off the platform, return them to the platform.
  45.             if (transform.position.y < -7)
  46.             {
  47.                 transform.position = new Vector3(0, 2.7f, 0);
  48.             }
  49.         }
  50.  
  51.         //If escape is pressed, quit the game.
  52.         if (Input.GetKeyDown(KeyCode.Escape))
  53.         {
  54.             Application.Quit();
  55.         }
  56.  
  57.        
  58.  
  59.         //No skateboard? Fine, walking speed then (5.0f, not extremely slow, but infuriating)
  60.         else
  61.         {
  62.             speed = 5.0F;
  63.         }
  64.  
  65.  
  66.         //Applying gravity to the controller
  67.         moveDirection.y -= gravity * Time.deltaTime;
  68.  
  69.         //Making the character move
  70.         controller.Move(moveDirection * Time.deltaTime);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement