Facebook
From Colossal Treeshrew, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 93
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Movement : MonoBehaviour {
  6.  
  7.     public float movementSpeed;
  8.     public float rotationSpeed;
  9.     public float rotX;
  10.     public float rotY;
  11.     public float rotZ;
  12.     public Rigidbody rb;
  13.     public bool playerIsOnTheGround = true;
  14.  
  15.     // Use this for initialization
  16.     private void Start() {
  17.         rb = GetComponent<Rigidbody>();
  18.     }
  19.  
  20.     //Update is called once per frame
  21.     void FixedUpdate() {
  22.  
  23.         if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey("w")) {
  24.             transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed * 2.5f;
  25.         } else if (Input.GetKey("w") && !Input.GetKey(KeyCode.LeftShift)) {
  26.             transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed;
  27.         } else if (Input.GetKey("s")) {
  28.             transform.position -= transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed;
  29.         }
  30.  
  31.         if (Input.GetKey("a") && !Input.GetKey("d")) {
  32.             transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * movementSpeed;
  33.         } else if (Input.GetKey("d") && !Input.GetKey("a")) {
  34.             transform.position -= transform.TransformDirection(Vector3.left) * Time.deltaTime * movementSpeed;
  35.         }
  36.     }
  37.  
  38.     void Update() {
  39.         if (Input.GetButtonDown("Jump") && playerIsOnTheGround)
  40.         {
  41.             rb.AddForce(new Vector3(0, 5, 0), ForceMode.Impulse);
  42.             playerIsOnTheGround = false;
  43.         }
  44.  
  45.         private void OnCollisionEnter(Collision collision)
  46.         {
  47.  
  48.             if (collision.gameObject.name == "Floor")
  49.             {
  50.  
  51.                 playerIsOnTheGround = true;
  52.             }
  53.         }
  54.      
  55.         rotX -= Input.GetAxis ("Mouse Y") * Time.deltaTime * rotationSpeed;
  56.         rotY += Input.GetAxis ("Mouse X") * Time.deltaTime * rotationSpeed;
  57.  
  58.         if (rotX < -90) {
  59.             rotX = -90;
  60.         }   else if (rotX > 90) {
  61.             rotX = 90;
  62.         }
  63.    
  64.         transform.rotation = Quaternion.Euler (0, rotY, 0);
  65.         GameObject.FindWithTag  ("MainCamera").transform.rotation = Quaternion.Euler (rotX, rotY, 0);
  66.     }
  67. }