From 0382584a77aed5dc926909b7dad6159ae0757e38 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Mon, 20 Apr 2020 13:23:57 -0400 Subject: Added jumping and gun (not functional) --- Assets/Scripts/Exit.cs | 17 ++++++++++++ Assets/Scripts/Exit.cs.meta | 11 ++++++++ Assets/Scripts/MouseLook.cs | 10 ++++++- Assets/Scripts/PlayerMovement.cs | 51 +++++++++++++++++++++++++++++++++++ Assets/Scripts/PlayerMovement.cs.meta | 11 ++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 Assets/Scripts/Exit.cs create mode 100644 Assets/Scripts/Exit.cs.meta create mode 100644 Assets/Scripts/PlayerMovement.cs create mode 100644 Assets/Scripts/PlayerMovement.cs.meta (limited to 'Assets/Scripts') diff --git a/Assets/Scripts/Exit.cs b/Assets/Scripts/Exit.cs new file mode 100644 index 0000000..a0feb04 --- /dev/null +++ b/Assets/Scripts/Exit.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Exit : MonoBehaviour +{ + // Update is called once per frame + void Update() + { + if (Input.GetKey("escape")) + { + Debug.Log("Game is now closed."); + Application.Quit(); + } + + } +} diff --git a/Assets/Scripts/Exit.cs.meta b/Assets/Scripts/Exit.cs.meta new file mode 100644 index 0000000..87fc2df --- /dev/null +++ b/Assets/Scripts/Exit.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3aa4285199d3f6f3a88d2b5993d8adfd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/MouseLook.cs b/Assets/Scripts/MouseLook.cs index f52f685..3365145 100644 --- a/Assets/Scripts/MouseLook.cs +++ b/Assets/Scripts/MouseLook.cs @@ -6,11 +6,13 @@ public class MouseLook : MonoBehaviour public float mouseSensitivity = 100f; public Transform playerBody; + + float xRotation = 0f; // Start is called before the first frame update void Start() { - + Cursor.lockState = CursorLockMode.Locked; } // Update is called once per frame @@ -18,6 +20,12 @@ public class MouseLook : MonoBehaviour { float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; + + xRotation -= mouseY; + + xRotation = Mathf.Clamp(xRotation, -90f, 90f); + + transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); playerBody.Rotate(Vector3.up * mouseX); diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs new file mode 100644 index 0000000..b0b6874 --- /dev/null +++ b/Assets/Scripts/PlayerMovement.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PlayerMovement : MonoBehaviour +{ + public CharacterController controller; + + public float speed = 12f; + + public float gravity = -9.81f; + + public float jumpHeight = 3f; + + public Transform groundCheck; + + public float groundDistance = 0.4f; + + public LayerMask groundMask; + + Vector3 velocity; + bool isGrounded; + + // Update is called once per frame + void Update() + { + isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask); + + if (isGrounded && velocity.y < 0) + { + velocity.y = -2f; + } + + float x = Input.GetAxis("Horizontal"); + float z = Input.GetAxis("Vertical"); + + Vector3 move = transform.right * x + transform.forward * z; + + controller.Move(move * speed * Time.deltaTime); + + if (Input.GetButtonDown("Jump") && isGrounded) + { + velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); + } + + velocity.y += gravity * Time.deltaTime; + + controller.Move(velocity * Time.deltaTime); + + } +} diff --git a/Assets/Scripts/PlayerMovement.cs.meta b/Assets/Scripts/PlayerMovement.cs.meta new file mode 100644 index 0000000..7cb0f4d --- /dev/null +++ b/Assets/Scripts/PlayerMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0230ac989e3d56e25a60a15a70f53fd4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.2.3