Added enemy, fixed spawn logic

This commit is contained in:
2025-08-02 16:04:33 +02:00
parent 4bf2804650
commit 3efe2070c4
16 changed files with 346 additions and 30 deletions

View File

@@ -734,7 +734,8 @@ MonoBehaviour:
- {fileID: 6519413531553640820}
- {fileID: 4973078823863902098}
- {fileID: 7903349254928241710}
test: {fileID: 6105862447941566498, guid: db659e1194f587c4d8dd952811175c5e, type: 3}
test: {fileID: 2103289545128957355, guid: 65c81f07bd3122d4da52f60b523516a3, type: 3}
allowSpawn: 1
--- !u!1 &2837174233086072514
GameObject:
m_ObjectHideFlags: 0

View File

@@ -373,6 +373,7 @@ MonoBehaviour:
- {fileID: 6980416409678530651}
- {fileID: 1325353513586617208}
test: {fileID: 6105862447941566498, guid: db659e1194f587c4d8dd952811175c5e, type: 3}
allowSpawn: 1
--- !u!1001 &627855985815857216
PrefabInstance:
m_ObjectHideFlags: 0
@@ -886,6 +887,7 @@ MonoBehaviour:
wallWest: {fileID: 0}
spawnPoints: []
test: {fileID: 0}
allowSpawn: 0
--- !u!4 &1813150499232309186 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 9e9d5761822c91f44b112d69a3ec09bd, type: 3}
@@ -1071,6 +1073,7 @@ MonoBehaviour:
wallWest: {fileID: 0}
spawnPoints: []
test: {fileID: 0}
allowSpawn: 0
--- !u!1001 &5264806082340469335
PrefabInstance:
m_ObjectHideFlags: 0
@@ -1154,6 +1157,7 @@ MonoBehaviour:
wallWest: {fileID: 0}
spawnPoints: []
test: {fileID: 0}
allowSpawn: 0
--- !u!4 &5664086053892742588 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 9e9d5761822c91f44b112d69a3ec09bd, type: 3}
@@ -1242,6 +1246,7 @@ MonoBehaviour:
wallWest: {fileID: 1324703056803136888}
spawnPoints: []
test: {fileID: 0}
allowSpawn: 0
--- !u!4 &6704669995139341385 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 9e9d5761822c91f44b112d69a3ec09bd, type: 3}

View File

@@ -448,6 +448,7 @@ MonoBehaviour:
- {fileID: 464892053139057970}
- {fileID: 2773562898438979010}
test: {fileID: 6105862447941566498, guid: db659e1194f587c4d8dd952811175c5e, type: 3}
allowSpawn: 1
--- !u!1 &9186255231503958152
GameObject:
m_ObjectHideFlags: 0

View File

@@ -247,6 +247,7 @@ MonoBehaviour:
- {fileID: 1287476518389190663}
- {fileID: 8670878002997374015}
test: {fileID: 6105862447941566498, guid: db659e1194f587c4d8dd952811175c5e, type: 3}
allowSpawn: 1
--- !u!1 &4664024420453137781
GameObject:
m_ObjectHideFlags: 0

View File

@@ -80,6 +80,7 @@ MonoBehaviour:
- {fileID: 5814503170226453088}
- {fileID: 5162249106696660312}
test: {fileID: 6105862447941566498, guid: db659e1194f587c4d8dd952811175c5e, type: 3}
allowSpawn: 1
--- !u!1 &5121653139323114344
GameObject:
m_ObjectHideFlags: 0

View File

@@ -248,6 +248,7 @@ MonoBehaviour:
spawnPoints:
- {fileID: 305529029419692106}
test: {fileID: 6105862447941566498, guid: db659e1194f587c4d8dd952811175c5e, type: 3}
allowSpawn: 1
--- !u!1001 &572179306618835033
PrefabInstance:
m_ObjectHideFlags: 0

View File

@@ -252,6 +252,7 @@ MonoBehaviour:
spawnPoints:
- {fileID: 8448325099915851505}
test: {fileID: 6105862447941566498, guid: db659e1194f587c4d8dd952811175c5e, type: 3}
allowSpawn: 1
--- !u!1001 &572179306618835033
PrefabInstance:
m_ObjectHideFlags: 0

View File

@@ -576,7 +576,7 @@ GameObject:
- component: {fileID: 1781483820777133190}
m_Layer: 0
m_Name: Player
m_TagString: Untagged
m_TagString: Player
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0

View File

@@ -25,6 +25,7 @@ public class RoomHandler : MonoBehaviour
[Header("Spawn Points")]
[SerializeField] private List<GameObject> spawnPoints;
[SerializeField] private GameObject test;
[SerializeField] private bool allowSpawn = false;
private readonly Dictionary<Side, DoorAnimation> doors = new();
@@ -64,23 +65,32 @@ public class RoomHandler : MonoBehaviour
public void SpawnEnemies(List<GameObject> enemyPrefabs)
{
int i = 0;
List<GameObject> enemyPrefabsLocal = new List<GameObject>(enemyPrefabs);
while (enemyPrefabsLocal.Count > 0)
if (!allowSpawn)
{
// Spawns enemy and removes it from the list
GameObject enemyPrefab = enemyPrefabsLocal[0];
enemyPrefabsLocal.RemoveAt(0);
// Select a spawn point with round-robin
Debug.Log("Ammount of spawn points: " + spawnPoints.Count);
GameObject spawnPoint = spawnPoints[i % spawnPoints.Count];
Instantiate(enemyPrefab, spawnPoint.transform.position + new Vector3(0, 1, 0), Quaternion.identity);
Debug.Log("Spawned enemy: " + enemyPrefab.name + " at " + spawnPoint.transform.position);
i++;
Debug.LogWarning("Enemy spawning is not allowed in this room.");
return;
}
else
{
int i = 0;
List<GameObject> enemyPrefabsLocal = new List<GameObject>(enemyPrefabs);
while (enemyPrefabsLocal.Count > 0)
{
// Spawns enemy and removes it from the list
GameObject enemyPrefab = enemyPrefabsLocal[0];
enemyPrefabsLocal.RemoveAt(0);
// Select a spawn point with round-robin
Debug.Log("Ammount of spawn points: " + spawnPoints.Count);
GameObject spawnPoint = spawnPoints[i % spawnPoints.Count];
Instantiate(enemyPrefab, spawnPoint.transform.position + new Vector3(0, 1, 0), Quaternion.identity);
Debug.Log("Spawned enemy: " + enemyPrefab.name + " at " + spawnPoint.transform.position);
i++;
}
}
}
void Start()
{

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 868ed703ae52cc148844ca68b0c454d9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,183 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2103289545128957355
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1593526044607189551}
- component: {fileID: 4260072969707999758}
- component: {fileID: 8136388527997030937}
m_Layer: 0
m_Name: Blob
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1593526044607189551
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2103289545128957355}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 9127254888608209968}
- {fileID: 4728640573077846392}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!143 &4260072969707999758
CharacterController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2103289545128957355}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 3
m_Height: 1
m_Radius: 1
m_SlopeLimit: 45
m_StepOffset: 0.3
m_SkinWidth: 0.08
m_MinMoveDistance: 0.001
m_Center: {x: 0, y: 0, z: 0}
--- !u!114 &8136388527997030937
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2103289545128957355}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 28c50bdf17c68be46a472d2e1959909c, type: 3}
m_Name:
m_EditorClassIdentifier:
charControl: {fileID: 4260072969707999758}
speed: 3
gravity: -9.81
jumpHeight: 1
groundCheck: {fileID: 4728640573077846392}
groundDistance: 0.4
groundMask:
serializedVersion: 2
m_Bits: 0
obstacleMask:
serializedVersion: 2
m_Bits: 0
detectionOffset: 0.5
player: {fileID: 0}
obstacleDetectionDistance: 1
rotationSpeed: 5
--- !u!1 &2485118932734020551
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4728640573077846392}
m_Layer: 0
m_Name: GroundCheck
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4728640573077846392
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2485118932734020551}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1593526044607189551}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1001 &8728042649825442779
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 1593526044607189551}
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalRotation.x
value: -0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalRotation.y
value: -0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalRotation.z
value: -0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 919132149155446097, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
propertyPath: m_Name
value: Blob_1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
--- !u!4 &9127254888608209968 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 96b30b0f854f6d44e81773a29ce920bf, type: 3}
m_PrefabInstance: {fileID: 8728042649825442779}
m_PrefabAsset: {fileID: 0}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 65c81f07bd3122d4da52f60b523516a3
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -129,7 +129,6 @@ GameObject:
m_Component:
- component: {fileID: 23489964}
- component: {fileID: 23489963}
- component: {fileID: 23489965}
m_Layer: 0
m_Name: Dungeon
m_TagString: Untagged
@@ -177,18 +176,6 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &23489965
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 23489962}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5520a437c5690d24695f9830bbd741b0, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1862002347
GameObject:
m_ObjectHideFlags: 0

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 723d291e577378b4da5446c434d8093e
folderAsset: yes
DefaultImporter:
externalObjects: {}
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: