Advertisement
Guest User

Untitled

a guest
Nov 11th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerController : MonoBehaviour
  5. {
  6.     //Variables
  7.     public float speed = 6.0F;
  8.     public float jumpSpeed = 8.0F;
  9.     public float gravity = 20.0F;
  10.     private Vector3 moveDirection = Vector3.zero;
  11.  
  12.     void Update()
  13.     {
  14.         CharacterController controller = GetComponent<CharacterController>();
  15.         // is the controller on the ground?
  16.         if (controller.isGrounded)
  17.         {
  18.             //Feed moveDirection with input.
  19.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  20.             moveDirection = transform.TransformDirection(moveDirection);
  21.             //Multiply it by speed.
  22.             moveDirection *= speed;
  23.             //Jumping
  24.             if (Input.GetButton("Jump"))
  25.                 moveDirection.y = jumpSpeed;
  26.  
  27.             if (transform.position.y < -7)
  28.             {
  29.                 transform.position = new Vector3(0, 2.7f, 0);
  30.             }
  31.         }
  32.  
  33.        
  34.         //Applying gravity to the controller
  35.         moveDirection.y -= gravity * Time.deltaTime;
  36.         //Making the character move
  37.         controller.Move(moveDirection * Time.deltaTime);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement