Basic game completed

This commit is contained in:
2024-12-09 23:16:34 +01:00
parent 10b694496b
commit 3b30d20e62
457 changed files with 34712 additions and 11643 deletions

View 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!");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c11d6352ad31a88488872ed000f18e43
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 28c50bdf17c68be46a472d2e1959909c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View 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);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f3bd2427246126e4ab2548543eac8290
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: