Creating a game like Granny in Unity involves making a horror escape game where you need to avoid a character (the "Granny") while solving puzzles and escaping. The gameplay revolves around stealth, tension, and exploration. I'll guide you through the process step-by-step, and provide some basic code snippets to get you started.
Step 1: Setting Up the Project
-
Create a New Unity Project:
- Open Unity Hub.
- Click on New Project and select the 3D template.
- Name your project (e.g., "GrannyClone") and choose the location where you want to save the project.
- Click Create.
-
Set Up the Scene:
- Add a Floor: Right-click in the Hierarchy and create a 3D Object > Plane. This will be your floor.
- Create Walls: You can create the walls by adding more 3D Objects > Cube and resizing them to form walls.
- Add Lighting: If not already added, create a light source like a Directional Light to brighten your scene.
Step 2: The Player Character
-
Add Player Model:
- You can use a placeholder model for now, like a capsule. To do this, right-click in the Hierarchy and create a 3D Object > Capsule.
- Rename it Player.
- Add a Rigidbody and Capsule Collider to this object.
- Create a new script
PlayerController.cs
and attach it to the Player object.
-
Player Movement Script:
Create a PlayerController.cs
script to handle player movement:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float lookSpeed = 2f;
private float rotationX = 0;
public Camera playerCamera;
void Update()
{
MovePlayer();
LookAround();
}
void MovePlayer()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(horizontal, 0, vertical).normalized;
if (moveDirection.magnitude >= 0.1f)
{
transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
}
}
void LookAround()
{
float mouseX = Input.GetAxis("Mouse X") * lookSpeed;
float mouseY = Input.GetAxis("Mouse Y") * lookSpeed;
rotationX -= mouseY;
rotationX = Mathf.Clamp(rotationX, -60f, 60f);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.Rotate(Vector3.up * mouseX);
}
}
- This script allows for basic movement and camera control using WASD for movement and mouse for looking around.
- Make sure to assign the Camera to the
playerCamera
variable in the Inspector.
Step 3: Granny AI (The Enemy)
Now, let’s create the Granny AI that will chase the player.
-
Granny Model: You can use a placeholder model or download a simple character model from online assets (or make one using cubes).
-
Granny AI Script:
Create a script GrannyAI.cs
that will make Granny move towards the player.
using UnityEngine;
using UnityEngine.AI;
public class GrannyAI : MonoBehaviour
{
public Transform player;
public float detectionRange = 10f;
private NavMeshAgent navMeshAgent;
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
float distanceToPlayer = Vector3.Distance(transform.position, player.position);
if (distanceToPlayer <= detectionRange)
{
navMeshAgent.SetDestination(player.position);
}
}
}
- This script makes Granny detect the player within a detectionRange and then move towards the player using Unity's NavMeshAgent.
- Make sure to bake the NavMesh for the scene (Window > AI > Navigation).
- Assign the Player object to the
player
variable in the Inspector.
Step 4: Stealth Mechanism (Hiding and Sound)
To add a tension factor like stealth and sound:
-
Hiding Mechanism: Let's create an area where the player can hide.
- Add an object (e.g., a wardrobe or closet) that the player can enter to hide.
- Create a trigger area in the object where the player can "hide."
-
Sound: You can make Granny's footsteps or other sounds more suspenseful.
For the player hiding:
using UnityEngine;
public class HideInCabinet : MonoBehaviour
{
public GameObject player;
public bool isHiding = false;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
isHiding = true;
player.SetActive(false); // Hide player model
Debug.Log("Player is hiding!");
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
isHiding = false;
player.SetActive(true); // Unhide player model
Debug.Log("Player is out of hiding!");
}
}
}
This script makes the player hide in a specific area (for example, inside a cabinet). When the player enters the trigger, the player’s model is disabled (hidden), and when they exit, it’s re-enabled.
Step 5: Creating a Simple Win Condition
For a win condition (escaping the house), you can use an exit door that the player needs to unlock.
- Exit Door: Create a 3D Object > Cube to represent the exit door.
- Unlock Mechanism: The player needs to find a key to unlock the door.
Create a script ExitDoor.cs
:
using UnityEngine;
public class ExitDoor : MonoBehaviour
{
public bool isUnlocked = false;
public GameObject key;
void Update()
{
if (isUnlocked)
{
// Open the door (You could animate this)
Debug.Log("Door is unlocked!");
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player") && isUnlocked)
{
Debug.Log("You Win! Escape!");
// Implement win condition (e.g., load next level or end game)
}
}
public void UnlockDoor()
{
isUnlocked = true;
}
}
The player can unlock the door if they pick up a key. You could add a Key
object somewhere in the scene, and when the player collides with it, it calls UnlockDoor()
to enable the exit.
Step 6: Finalizing the Game
-
Lighting and Atmosphere:
- Add spooky lighting and shadows to enhance the horror feel.
- Use spotlights or point lights for flashlight effects if you want the player to use a flashlight.
-
Audio:
- Add eerie background music or footsteps for Granny and the player.
- Use Unity's AudioSource for playing sound effects at specific moments (like a door creaking).
-
Game UI:
- Add a basic UI showing the player’s health (if you have damage).
- Display a countdown timer or a message for win conditions.
-
NavMesh and Pathfinding:
- Make sure the entire scene is baked with the NavMesh so Granny can navigate the house properly.
Final Thoughts:
This guide provides the basic framework for creating a Granny-style horror game in Unity. The core mechanics like player movement, AI behavior, hiding, and win conditions are covered here, but you can add more features like puzzles, more advanced AI, level design, and more.
You can continually polish the game by adding animations, improving Granny's AI, or including more complex puzzles and items for the player to interact with.
-Om Bawne