Basic game completed
This commit is contained in:
8
3D FPS/Assets/Scripts/Editor.meta
Normal file
8
3D FPS/Assets/Scripts/Editor.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49a8034718543ac4a9a2a1f504cd9082
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
3D FPS/Assets/Scripts/Enemy.meta
Normal file
8
3D FPS/Assets/Scripts/Enemy.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4598eeef18a1d343881614e22d3603f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
69
3D FPS/Assets/Scripts/Enemy/EnemyAttack.cs
Normal file
69
3D FPS/Assets/Scripts/Enemy/EnemyAttack.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyAttack : MonoBehaviour
|
||||
{
|
||||
public float attackRange = 2f;
|
||||
public float attackAngle = 45f;
|
||||
public float attackCooldown = 2f;
|
||||
public int damage = 10;
|
||||
|
||||
private Transform player;
|
||||
private float lastAttackTime;
|
||||
void Start()
|
||||
{
|
||||
GameObject playerObject = GameObject.FindWithTag("Player");
|
||||
if (playerObject != null)
|
||||
{
|
||||
player = playerObject.transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Player not found! Ensure the player has the tag 'Player'.");
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (player == null) return;
|
||||
|
||||
if (IsPlayerInFront() && IsPlayerInRange())
|
||||
{
|
||||
if (Time.time >= lastAttackTime + attackCooldown)
|
||||
{
|
||||
AttackPlayer();
|
||||
lastAttackTime = Time.time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsPlayerInFront()
|
||||
{
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
|
||||
float angle = Vector3.Angle(transform.forward, directionToPlayer);
|
||||
return angle <= attackAngle / 2;
|
||||
}
|
||||
|
||||
private bool IsPlayerInRange()
|
||||
{
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
|
||||
return distanceToPlayer <= attackRange;
|
||||
}
|
||||
|
||||
private void AttackPlayer()
|
||||
{
|
||||
Debug.Log("Enemy attacks the player!");
|
||||
|
||||
PlayerHealth playerHealth = player.GetComponent<PlayerHealth>();
|
||||
if (playerHealth != null)
|
||||
{
|
||||
playerHealth.Health -= damage;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("PlayerHealth script not found on the player!");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/Enemy/EnemyAttack.cs.meta
Normal file
11
3D FPS/Assets/Scripts/Enemy/EnemyAttack.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c11d6352ad31a88488872ed000f18e43
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
91
3D FPS/Assets/Scripts/Enemy/EnemyMovement.cs
Normal file
91
3D FPS/Assets/Scripts/Enemy/EnemyMovement.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyMovement : MonoBehaviour
|
||||
{
|
||||
public CharacterController charControl;
|
||||
|
||||
public float speed = 12f;
|
||||
public float gravity = -9.81f;
|
||||
public float jumpHeight = 3f;
|
||||
|
||||
|
||||
|
||||
public Transform groundCheck;
|
||||
public float groundDistance = 0.4f;
|
||||
public LayerMask groundMask;
|
||||
public LayerMask obstacleMask;
|
||||
public float detectionOffset = 0.5f;
|
||||
|
||||
public Transform player;
|
||||
public float obstacleDetectionDistance = 1f;
|
||||
public float rotationSpeed = 5f;
|
||||
|
||||
private Vector3 velocity;
|
||||
private bool isGrounded;
|
||||
|
||||
void Start()
|
||||
{
|
||||
GameObject playerObject = GameObject.FindWithTag("Player");
|
||||
if (playerObject != null)
|
||||
{
|
||||
player = playerObject.transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Player not found in the scene! Ensure the player GameObject is tagged as 'Player'.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(player == null) return;
|
||||
|
||||
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
|
||||
|
||||
if(isGrounded && velocity.y < 0)
|
||||
{
|
||||
velocity.y = -2f;
|
||||
}
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
Vector3 move = new Vector3(directionToPlayer.x, 0, directionToPlayer.z);
|
||||
|
||||
Vector3 rayOrigin = transform.position + Vector3.down * detectionOffset;
|
||||
|
||||
bool isBlocked = Physics.Raycast(rayOrigin, move, obstacleDetectionDistance, obstacleMask);
|
||||
|
||||
if (isBlocked && isGrounded)
|
||||
{
|
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
}
|
||||
|
||||
charControl.Move(move * speed * Time.deltaTime);
|
||||
|
||||
velocity.y += gravity * Time.deltaTime;
|
||||
|
||||
charControl.Move(velocity * Time.deltaTime);
|
||||
|
||||
RotateTowardsPlayer(directionToPlayer);
|
||||
}
|
||||
|
||||
private void RotateTowardsPlayer(Vector3 directionToPlayer)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(new Vector3(directionToPlayer.x, 0, directionToPlayer.z));
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (player != null)
|
||||
{
|
||||
Vector3 directionToPlayer = (player.position - transform.position).normalized;
|
||||
Vector3 moveDirection = new Vector3(directionToPlayer.x, 0, directionToPlayer.z);
|
||||
Vector3 rayOrigin = transform.position + Vector3.down * detectionOffset;
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawRay(rayOrigin, moveDirection * obstacleDetectionDistance);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/Enemy/EnemyMovement.cs.meta
Normal file
11
3D FPS/Assets/Scripts/Enemy/EnemyMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 28c50bdf17c68be46a472d2e1959909c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
47
3D FPS/Assets/Scripts/Enemy/RandomSpawn.cs
Normal file
47
3D FPS/Assets/Scripts/Enemy/RandomSpawn.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class RandomSpawn : MonoBehaviour
|
||||
{
|
||||
public GameObject enemyPrefab;
|
||||
public float spawnInterval = 5f;
|
||||
|
||||
public Vector3 spawnAreaCenter;
|
||||
public Vector3 spawnAreaSize;
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(SpawnEnemies());
|
||||
}
|
||||
|
||||
|
||||
IEnumerator SpawnEnemies()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
yield return new WaitForSeconds(spawnInterval);
|
||||
SpawnEnemy();
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnEnemy()
|
||||
{
|
||||
Vector3 randomPosition = GetRandomPosition();
|
||||
Instantiate(enemyPrefab, randomPosition, Quaternion.identity);
|
||||
}
|
||||
|
||||
Vector3 GetRandomPosition()
|
||||
{
|
||||
float x = Random.Range(spawnAreaCenter.x - spawnAreaSize.x / 2, spawnAreaCenter.x + spawnAreaSize.x / 2);
|
||||
float y = spawnAreaCenter.y;
|
||||
float z = Random.Range(spawnAreaCenter.z - spawnAreaSize.z / 2, spawnAreaCenter.z + spawnAreaSize.z / 2);
|
||||
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawWireCube(spawnAreaCenter, spawnAreaSize);
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/Enemy/RandomSpawn.cs.meta
Normal file
11
3D FPS/Assets/Scripts/Enemy/RandomSpawn.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3bd2427246126e4ab2548543eac8290
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
3D FPS/Assets/Scripts/Entities.meta
Normal file
8
3D FPS/Assets/Scripts/Entities.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bde0ba6711acdb64e8a9923867cd17d6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
45
3D FPS/Assets/Scripts/Entities/Entity.cs
Normal file
45
3D FPS/Assets/Scripts/Entities/Entity.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class Entity : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float startingHealth;
|
||||
[SerializeField] private int scoreValue;
|
||||
private float health;
|
||||
private Transform player;
|
||||
|
||||
public float Health
|
||||
{
|
||||
get
|
||||
{
|
||||
return health;
|
||||
}
|
||||
set
|
||||
{
|
||||
if(value <= 0f)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
PlayerHealth playerHealth = player.GetComponent<PlayerHealth>();
|
||||
playerHealth.AddScore(scoreValue);
|
||||
}
|
||||
else health = value;
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Health = startingHealth;
|
||||
|
||||
GameObject playerObject = GameObject.FindWithTag("Player");
|
||||
if (playerObject != null)
|
||||
{
|
||||
player = playerObject.transform;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Player not found! Ensure the player has the tag 'Player'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/Entities/Entity.cs.meta
Normal file
11
3D FPS/Assets/Scripts/Entities/Entity.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1b4d54af625597c4786158cd650f5d17
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
27
3D FPS/Assets/Scripts/GameOverScreen.cs
Normal file
27
3D FPS/Assets/Scripts/GameOverScreen.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEditor.SearchService;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
|
||||
|
||||
public class GameOverScreen : MonoBehaviour
|
||||
{
|
||||
public TextMeshProUGUI scoreText;
|
||||
public void TriggerScreen(int score = 0)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
scoreText.text = "SCORE: " + score.ToString();
|
||||
gameObject.SetActive(true);
|
||||
|
||||
Time.timeScale = 0;
|
||||
}
|
||||
|
||||
public void RestartButton()
|
||||
{
|
||||
Time.timeScale = 1;
|
||||
SceneManager.LoadScene("SampleScene");
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/GameOverScreen.cs.meta
Normal file
11
3D FPS/Assets/Scripts/GameOverScreen.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3ffa23fd2c86874438b02dfee1921a6c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
3D FPS/Assets/Scripts/Gun.meta
Normal file
8
3D FPS/Assets/Scripts/Gun.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 046c2e131ec728d4db85017351c60e8c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
37
3D FPS/Assets/Scripts/Gun/GunDamage.cs
Normal file
37
3D FPS/Assets/Scripts/Gun/GunDamage.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
public class GunDamage : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject bulletHolePrefab;
|
||||
public float damage;
|
||||
public float bulletRange;
|
||||
private Transform playerCam;
|
||||
|
||||
public AudioSource audioSource;
|
||||
public AudioClip gunShot;
|
||||
void Start()
|
||||
{
|
||||
playerCam = Camera.main.transform;
|
||||
}
|
||||
public void Shoot()
|
||||
{
|
||||
Ray gunRay = new Ray(playerCam.position, playerCam.forward);
|
||||
if(Physics.Raycast(gunRay, out RaycastHit hitInfo, bulletRange))
|
||||
{
|
||||
if(hitInfo.collider.gameObject.TryGetComponent(out Entity enemy))
|
||||
{
|
||||
enemy.Health -= damage;
|
||||
}
|
||||
else
|
||||
{
|
||||
GameObject obj = Instantiate(bulletHolePrefab, hitInfo.point, Quaternion.LookRotation(hitInfo.normal));
|
||||
obj.transform.position += obj.transform.forward / 1000;
|
||||
}
|
||||
}
|
||||
|
||||
audioSource.PlayOneShot(gunShot);
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/Gun/GunDamage.cs.meta
Normal file
11
3D FPS/Assets/Scripts/Gun/GunDamage.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d9a3d273a07acbc4985f0c9ef1c762e2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
3D FPS/Assets/Scripts/Player.meta
Normal file
8
3D FPS/Assets/Scripts/Player.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 018652128e98a2f4ca26ce6aaea4d105
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
46
3D FPS/Assets/Scripts/Player/PlayerHealth.cs
Normal file
46
3D FPS/Assets/Scripts/Player/PlayerHealth.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor.UI;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
public class PlayerHealth : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float startingHealth;
|
||||
private float health;
|
||||
public TextMeshProUGUI healthMonitor;
|
||||
public GameOverScreen gameOverScreen;
|
||||
private int score = 0;
|
||||
|
||||
public float Health
|
||||
{
|
||||
get
|
||||
{
|
||||
return health;
|
||||
}
|
||||
set
|
||||
{
|
||||
health = value;
|
||||
healthMonitor.text = "Health: " + health;
|
||||
if(health <= 0f)
|
||||
{
|
||||
playerDestroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
Health = startingHealth;
|
||||
}
|
||||
|
||||
private void playerDestroy()
|
||||
{
|
||||
gameOverScreen.TriggerScreen(score);
|
||||
}
|
||||
|
||||
public void AddScore(int n)
|
||||
{
|
||||
score += n;
|
||||
}
|
||||
}
|
||||
11
3D FPS/Assets/Scripts/Player/PlayerHealth.cs.meta
Normal file
11
3D FPS/Assets/Scripts/Player/PlayerHealth.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 479c0ccaabee4134cbe1aa7729b8ef75
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user