diff --git a/Assets/Scripts.meta b/Assets/Scripts.meta new file mode 100644 index 0000000..8777b3b --- /dev/null +++ b/Assets/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1037018fb69caf541b3dfb87aa3bbbc5 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs new file mode 100644 index 0000000..b91aa79 --- /dev/null +++ b/Assets/Scripts/PlayerMovement.cs @@ -0,0 +1,72 @@ +using UnityEngine; + +[RequireComponent(typeof(CharacterController))] +public class PlayerMovement : MonoBehaviour +{ + public float speed = 7.5f; + public float sprintSpeed = 11.5f; + public float jumpSpeed = 8.0f; + public float gravity = 20.0f; + public Camera playerCamera; + public float lookSpeed = 2.0f; + public float lookXLimit = 45.0f; + + private CharacterController characterController; + private Vector3 moveDirection = Vector3.zero; + private float rotationX = 0; + + [HideInInspector] + public bool canMove = true; + + void Start() + { + characterController = GetComponent(); + + // Lock cursor + Cursor.lockState = CursorLockMode.Locked; + Cursor.visible = false; + } + + void Update() + { + // We are grounded, so recalculate move direction based on axes + Vector3 forward = transform.TransformDirection(Vector3.forward); + Vector3 right = transform.TransformDirection(Vector3.right); + + // Press Left Shift to sprint + bool isSprinting = Input.GetKey(KeyCode.LeftShift); + float curSpeedX = canMove ? (isSprinting ? sprintSpeed : speed) * Input.GetAxis("Vertical") : 0; + float curSpeedY = canMove ? (isSprinting ? sprintSpeed : speed) * Input.GetAxis("Horizontal") : 0; + float movementDirectionY = moveDirection.y; + moveDirection = (forward * curSpeedX) + (right * curSpeedY); + + if (Input.GetButton("Jump") && canMove && characterController.isGrounded) + { + moveDirection.y = jumpSpeed; + } + else + { + moveDirection.y = movementDirectionY; + } + + // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below + // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied + // as an acceleration (ms^-2) + if (!characterController.isGrounded) + { + moveDirection.y -= gravity * Time.deltaTime; + } + + // Move the controller + characterController.Move(moveDirection * Time.deltaTime); + + // Player and Camera rotation + if (canMove) + { + rotationX += -Input.GetAxis("Mouse Y") * lookSpeed; + rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit); + playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0); + transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0); + } + } +} diff --git a/Assets/Scripts/PlayerMovement.cs.meta b/Assets/Scripts/PlayerMovement.cs.meta new file mode 100644 index 0000000..52e7c6e --- /dev/null +++ b/Assets/Scripts/PlayerMovement.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0dd0f6132788a3e45b5289b710fdc8fb \ No newline at end of file diff --git a/Assets/Scripts/PlayerShooting.cs b/Assets/Scripts/PlayerShooting.cs new file mode 100644 index 0000000..64595ad --- /dev/null +++ b/Assets/Scripts/PlayerShooting.cs @@ -0,0 +1,81 @@ +using UnityEngine; + +public class PlayerShooting : MonoBehaviour +{ + public int damagePerShot = 20; + public float timeBetweenBullets = 0.15f; + public float range = 100f; + + float timer; + Ray shootRay = new Ray(); + RaycastHit shootHit; + int shootableMask; + ParticleSystem gunParticles; + LineRenderer gunLine; + AudioSource gunAudio; + Light gunLight; + float effectsDisplayTime = 0.2f; + + void Awake() + { + shootableMask = LayerMask.GetMask("Shootable"); + gunParticles = GetComponent(); + gunLine = GetComponent(); + gunAudio = GetComponent(); + gunLight = GetComponent(); + } + + void Update() + { + timer += Time.deltaTime; + + if (Input.GetButton("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) + { + Shoot(); + } + + if (timer >= timeBetweenBullets * effectsDisplayTime) + { + DisableEffects(); + } + } + + public void DisableEffects() + { + gunLine.enabled = false; + gunLight.enabled = false; + } + + void Shoot() + { + timer = 0f; + + gunAudio.Play(); + + gunLight.enabled = true; + + gunParticles.Stop(); + gunParticles.Play(); + + gunLine.enabled = true; + gunLine.SetPosition(0, transform.position); + + shootRay.origin = transform.position; + shootRay.direction = transform.forward; + + if (Physics.Raycast(shootRay, out shootHit, range, shootableMask)) + { + // We hit a shootable object + Target target = shootHit.collider.GetComponent(); + if (target != null) + { + target.TakeDamage(damagePerShot); + } + gunLine.SetPosition(1, shootHit.point); + } + else + { + gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range); + } + } +} diff --git a/Assets/Scripts/PlayerShooting.cs.meta b/Assets/Scripts/PlayerShooting.cs.meta new file mode 100644 index 0000000..c1f4d4d --- /dev/null +++ b/Assets/Scripts/PlayerShooting.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 9d58a8551ce33d64fb4d691591a5bcea \ No newline at end of file diff --git a/Assets/Scripts/Target.cs b/Assets/Scripts/Target.cs new file mode 100644 index 0000000..a3bc0f5 --- /dev/null +++ b/Assets/Scripts/Target.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +public class Target : MonoBehaviour +{ + public float health = 50f; + + public void TakeDamage(float amount) + { + health -= amount; + if (health <= 0f) + { + Die(); + } + } + + void Die() + { + // For now, we'll just destroy the target object + Destroy(gameObject); + } +} diff --git a/Assets/Scripts/Target.cs.meta b/Assets/Scripts/Target.cs.meta new file mode 100644 index 0000000..10c904a --- /dev/null +++ b/Assets/Scripts/Target.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 90b9cda00fa7d9d4d99d69691e8b4f12 \ No newline at end of file