add: Level and Game controllers base
update: scripts are now in folders
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class BulletProjectile : MonoBehaviour
|
||||
{
|
||||
public float speed = 1f;
|
||||
public float damage = 20f;
|
||||
public float timeToLive = 5f;
|
||||
|
||||
float currentTime = 0;
|
||||
|
||||
Rigidbody rb;
|
||||
|
||||
void Start() {
|
||||
rb = GetComponent<Rigidbody>();
|
||||
rb.linearVelocity = transform.forward * speed;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
currentTime += Time.deltaTime;
|
||||
if (currentTime >= timeToLive) Destroy(gameObject);
|
||||
}
|
||||
|
||||
void OnTriggerEnter(Collider other) {
|
||||
if (other.gameObject.CompareTag("Enemy")) {
|
||||
Enemy enemy = other.GetComponent<Enemy>();
|
||||
enemy.DealDamage(damage);
|
||||
}
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e7a451ad0b53e8c0ca82f7ce15114c38
|
||||
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraMovement : MonoBehaviour
|
||||
{
|
||||
public float horSpeed = 20f;
|
||||
public float verSpeed = 20f;
|
||||
public float verUpperBound = 45f;
|
||||
public float verLowerBound = -45f;
|
||||
|
||||
private float verRotation = 0f;
|
||||
private Camera cam;
|
||||
|
||||
void Start() {
|
||||
cam = GetComponentInChildren<Camera>();
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
float horRotation = Input.GetAxis("Mouse X") * Time.deltaTime * horSpeed;
|
||||
transform.Rotate(0, horRotation, 0);
|
||||
|
||||
verRotation -= Input.GetAxis("Mouse Y") * Time.deltaTime * verSpeed;
|
||||
verRotation = Mathf.Clamp(verRotation, verLowerBound, verUpperBound);
|
||||
cam.transform.localEulerAngles = new(verRotation, 0, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1241dd14aac57cb40a0e8493ced10162
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Pistol : Weapon
|
||||
{
|
||||
public GameObject projectilePrefab;
|
||||
public Transform spawnPosition;
|
||||
public float fireRate = 1f;
|
||||
|
||||
float currentDelay = 0;
|
||||
|
||||
public override void Shoot(bool keyDown, bool keyHold) {
|
||||
if (keyDown && (fireRate == 0 || currentDelay == 1 / fireRate)) {
|
||||
Instantiate(projectilePrefab, spawnPosition.position, spawnPosition.rotation);
|
||||
currentDelay = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if (fireRate != 0) currentDelay = Mathf.Clamp(currentDelay += Time.deltaTime, 0, 1 / fireRate);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b5e56940d7855caeb2d584265db0624
|
||||
@@ -0,0 +1,79 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[Header("Horizontal movement")]
|
||||
public float speed = 1f;
|
||||
public float sprintSpeedMultiplier = 2f;
|
||||
public float crouchSpeedMultiplier = 0.5f;
|
||||
public float crouchHeightMultiplier = 0.5f;
|
||||
[Header("Vertical movement")]
|
||||
public float jumpForce = 1f;
|
||||
public float gravityMultiplier = 1f;
|
||||
public Transform legsPosition;
|
||||
public float legsRadius = 0.5f;
|
||||
public LayerMask groundMask;
|
||||
|
||||
[Header("Debug info")]
|
||||
[SerializeField] bool isCrouching = false;
|
||||
[SerializeField] Vector3 gravityVelocity;
|
||||
[SerializeField] bool grounded;
|
||||
|
||||
CharacterController characterController;
|
||||
|
||||
void Start() {
|
||||
characterController = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
grounded = GetOnGround();
|
||||
HandleCrouching();
|
||||
HandleMovement();
|
||||
HandleJumping();
|
||||
DoGravity();
|
||||
}
|
||||
|
||||
void HandleCrouching() {
|
||||
if (Input.GetKeyDown(KeyCode.C)) {
|
||||
isCrouching = !isCrouching;
|
||||
if (isCrouching) {
|
||||
characterController.center += Vector3.down * (characterController.height * (1 - crouchHeightMultiplier)) / 2;
|
||||
characterController.height *= crouchHeightMultiplier;
|
||||
} else {
|
||||
characterController.height /= crouchHeightMultiplier;
|
||||
characterController.center -= Vector3.down * (characterController.height * (1 - crouchHeightMultiplier)) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleJumping() {
|
||||
if (Input.GetKey(KeyCode.Space) && grounded) {
|
||||
gravityVelocity = Vector3.up * (jumpForce * gravityMultiplier);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMovement() {
|
||||
float horInput = Input.GetAxis("Horizontal");
|
||||
float verInput = Input.GetAxis("Vertical");
|
||||
Vector3 movementVec = new(horInput, 0, verInput);
|
||||
movementVec = transform.TransformDirection(movementVec);
|
||||
movementVec = Vector3.ClampMagnitude(movementVec * speed, speed) * Time.deltaTime;
|
||||
float modifier = isCrouching ? crouchSpeedMultiplier : Input.GetKey(KeyCode.LeftShift) ? sprintSpeedMultiplier : 1;
|
||||
|
||||
characterController.Move(movementVec * modifier);
|
||||
}
|
||||
|
||||
void DoGravity() {
|
||||
if (grounded && gravityVelocity.y < 0) gravityVelocity = Vector3.zero;
|
||||
gravityVelocity += Physics.gravity * Time.deltaTime;
|
||||
characterController.Move(gravityVelocity * (Time.deltaTime * gravityMultiplier));
|
||||
}
|
||||
|
||||
bool GetOnGround() {
|
||||
return Physics.CheckSphere(legsPosition.position, legsRadius, groundMask);
|
||||
}
|
||||
|
||||
void OnControllerColliderHit(ControllerColliderHit hit) {
|
||||
hit.gameObject.SendMessage("ReverseOnControllerColliderHit", hit, SendMessageOptions.DontRequireReceiver);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecc089d81c2c4574693f263b2365fdc5
|
||||
@@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class Weapon : MonoBehaviour
|
||||
{
|
||||
public bool isActive { get; private set; } = true;
|
||||
|
||||
public abstract void Shoot(bool keyDown, bool keyHold);
|
||||
|
||||
public void SetWeaponState(bool active) {
|
||||
isActive = active;
|
||||
GetComponent<MeshRenderer>().enabled = active;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 918a4cc8f96795ab080e3de45b90632c
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class WeaponController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Weapon[] weapons;
|
||||
[SerializeField] int currentWeapon = 0;
|
||||
|
||||
void Start() {
|
||||
weapons = GetComponentsInChildren<Weapon>();
|
||||
for (int i = 0; i < weapons.Length; ++i) {
|
||||
if (i != currentWeapon) weapons[i].SetWeaponState(false);
|
||||
}
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if (Input.GetMouseButton(0) && weapons[currentWeapon].isActive) weapons[currentWeapon].Shoot(Input.GetMouseButtonDown(0), Input.GetMouseButton(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3724861d6ef4a112910a3ef14207757
|
||||
Reference in New Issue
Block a user