Completed:TeleportTrap && Write:GameManager
This commit is contained in:
49
Assets/Scripts/Trap/TeleportTrap.cs
Normal file
49
Assets/Scripts/Trap/TeleportTrap.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public class TeleportTrap : MonoBehaviour {
|
||||
|
||||
|
||||
private Vector3 GetRandomPosition() {
|
||||
Vector3 randomPosition;
|
||||
int attempts = 0;
|
||||
|
||||
do {
|
||||
// Generate random X and Z positions rounded to nearest 10, then offset by 5
|
||||
float x = Mathf.Round(Random.Range(-45f, 45f) / 10f) * 10f + 5f;
|
||||
float z = Mathf.Round(Random.Range(-45f, 45f) / 10f) * 10f + 5f;
|
||||
|
||||
// Fixed Y position for traps
|
||||
randomPosition = new Vector3(x, 3.5f, z);
|
||||
|
||||
attempts++;
|
||||
if (attempts > 100) {
|
||||
Debug.LogWarning("No valid position found for the death trap.");
|
||||
|
||||
// Exit if too many attempts are made
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (
|
||||
Physics.CheckBox(randomPosition, new Vector3(2.5f, 3.5f, 2.5f), Quaternion.identity, LayerMask.GetMask("Walls")) || // Check for walls
|
||||
Physics.CheckBox(randomPosition, new Vector3(2.5f, 3.5f, 2.5f), Quaternion.identity, LayerMask.GetMask("Collectible")) || // Check for collectibles
|
||||
Physics.CheckBox(randomPosition, new Vector3(2.5f, 3.5f, 2.5f), Quaternion.identity, LayerMask.GetMask("Player")) // Check for player
|
||||
);
|
||||
|
||||
// Return a valid position for the death trap
|
||||
return randomPosition;
|
||||
}
|
||||
|
||||
private Vector3 GetPositionTeleport() {
|
||||
Vector3 spawnPosition = GetRandomPosition();
|
||||
return spawnPosition;
|
||||
}
|
||||
|
||||
private void OnCollisionEnter(Collision collision)
|
||||
{
|
||||
if (collision.gameObject.CompareTag("Player"))
|
||||
{
|
||||
collision.transform.position = GetPositionTeleport();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Trap/TeleportTrap.cs.meta
Normal file
2
Assets/Scripts/Trap/TeleportTrap.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca1a9a3813a058946990c84846f54c17
|
||||
Reference in New Issue
Block a user