Hazards and obstacles make the game more challenging and engaging.
Step 1: Create a Hazard Object
Step 2: Create a Hazard Script
Attach this script to the hazard object:
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.CompareTag("Player")) {
Debug.Log("Player hit a hazard!");
}
}
Step 3: Reset Player Position on Collision
Modify the script to reset the player:
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.CompareTag("Player")) {
collision.gameObject.transform.position = new Vector3(0, 2, 0); // Reset position
}
}