Skip to content Skip to sidebar Skip to footer

Widget HTML #1

How to Create a 3D Survival Game in Godot 4.2 with GDScript

How to Create a 3D Survival Game in Godot 4.2 with GDScript

Welcome to our comprehensive course on creating a captivating 3D first-person survival game using the Godot Engine 4.2.1 and GDScript programming language.

Enroll Now

Creating a 3D survival game in Godot 4.2 with GDScript can be a rewarding experience, allowing you to craft a unique and immersive world. This guide will walk you through the essential steps, from setting up the project to implementing key gameplay features.

Setting Up Your Project

1. Install Godot

First, ensure you have Godot 4.2 installed on your machine. You can download it from the official Godot website. Install the appropriate version for your operating system.

2. Create a New Project

Open Godot and create a new project:

  1. Click on "New Project."
  2. Choose a project path and name your project, e.g., "3DSurvivalGame."
  3. Set the renderer to "Forward+" for better performance in 3D.

Basic Scene Setup

3. Create the Main Scene

Your main scene will serve as the game's starting point:

  1. Click on "Scene" and choose "New Scene."
  2. Add a "Spatial" node (renamed to "Main") as the root node.

4. Add a Player

Create a player character:

  1. Add a new "KinematicBody3D" node as a child of the "Main" node and rename it to "Player."
  2. Add a "CollisionShape3D" to the Player node and set its shape to "CapsuleShape3D."
  3. Add a "MeshInstance3D" to the Player node and set its mesh to a capsule for a visual representation.

5. Camera and Lighting

Add a camera to follow the player and some basic lighting:

  1. Add a "Camera3D" node as a child of the Player node.
  2. Add a "DirectionalLight3D" node to the Main node to illuminate the scene.

Get Started

Implementing Player Movement

6. Script the Player Movement

Attach a new script to the Player node and name it Player.gd:

gdscript
extends KinematicBody3D @export var speed := 5.0 @export var jump_strength := 10.0 @export var gravity := -9.8 var velocity := Vector3.ZERO func _physics_process(delta): var direction := Vector3.ZERO if Input.is_action_pressed("move_forward"): direction += -transform.basis.z if Input.is_action_pressed("move_backward"): direction += transform.basis.z if Input.is_action_pressed("move_left"): direction += -transform.basis.x if Input.is_action_pressed("move_right"): direction += transform.basis.x direction = direction.normalized() if is_on_floor(): if Input.is_action_just_pressed("jump"): velocity.y = jump_strength else: velocity.y = 0 else: velocity.y += gravity * delta velocity.x = direction.x * speed velocity.z = direction.z * speed velocity = move_and_slide(velocity, Vector3.UP)

7. Set Up Input Actions

Define input actions for movement:

  1. Go to "Project" -> "Project Settings" -> "Input Map."
  2. Add actions: move_forward, move_backward, move_left, move_right, and jump.
  3. Assign keys to these actions (e.g., W, A, S, D for movement and Space for jump).

Creating the Game World

8. Design the Terrain

Create a simple terrain using a heightmap:

  1. Add a new "MeshInstance3D" node to the Main node.
  2. Set its mesh to a "PlaneMesh" and scale it to a suitable size.
  3. Use a "HeightMap" terrain plugin or manually adjust the mesh for more complex terrain.

9. Add Environment Details

Add trees, rocks, and other environmental details:

  1. Import 3D models for these objects.
  2. Place them around the terrain to create a realistic environment.

Survival Mechanics

10. Health and Stamina

Add health and stamina systems to the player:

  1. In Player.gd, add variables for health and stamina:
gdscript
@export var max_health := 100 @export var max_stamina := 100 var health := max_health var stamina := max_stamina
  1. Create functions to handle health and stamina changes:
gdscript
func decrease_health(amount): health = max(health - amount, 0) func increase_health(amount): health = min(health + amount, max_health) func decrease_stamina(amount): stamina = max(stamina - amount, 0) func increase_stamina(amount): stamina = min(stamina + amount, max_stamina)

11. Implement Hunger and Thirst

Add hunger and thirst systems to increase the survival challenge:

  1. In Player.gd, add variables for hunger and thirst:
gdscript
@export var max_hunger := 100 @export var max_thirst := 100 var hunger := max_hunger var thirst := max_thirst
  1. Create functions to handle hunger and thirst changes:
gdscript
func decrease_hunger(amount): hunger = max(hunger - amount, 0) if hunger == 0: decrease_health(1) # Damage player if hunger is 0 func increase_hunger(amount): hunger = min(hunger + amount, max_hunger) func decrease_thirst(amount): thirst = max(thirst - amount, 0) if thirst == 0: decrease_health(1) # Damage player if thirst is 0 func increase_thirst(amount): thirst = min(thirst + amount, max_thirst)
  1. Add a timer to periodically decrease hunger and thirst:
gdscript
func _ready(): var hunger_timer = Timer.new() hunger_timer.wait_time = 60 # Decrease hunger every 60 seconds hunger_timer.connect("timeout", self, "_on_hunger_timeout") add_child(hunger_timer) hunger_timer.start() var thirst_timer = Timer.new() thirst_timer.wait_time = 45 # Decrease thirst every 45 seconds thirst_timer.connect("timeout", self, "_on_thirst_timeout") add_child(thirst_timer) thirst_timer.start() func _on_hunger_timeout(): decrease_hunger(1) func _on_thirst_timeout(): decrease_thirst(1)

Crafting System

12. Create Crafting Recipes

Define a crafting system where players can create items:

  1. Create a new script CraftingSystem.gd:
gdscript
extends Node class_name CraftingSystem var recipes := { "campfire": {"wood": 3, "stone": 2}, "spear": {"wood": 2, "stone": 1} } func can_craft(item, inventory): if item in recipes: for material in recipes[item]: if inventory[material] < recipes[item][material]: return false return true return false func craft(item, inventory): if can_craft(item, inventory): for material in recipes[item]: inventory[material] -= recipes[item][material] return true return false

13. Inventory System

Add an inventory to store resources:

  1. In Player.gd, add an inventory dictionary:
gdscript
var inventory := {"wood": 0, "stone": 0}
  1. Create functions to manage the inventory:
gdscript
func add_to_inventory(item, amount): if item in inventory: inventory[item] += amount else: inventory[item] = amount
  1. Use the crafting system to create items:
gdscript
func craft_item(item): var crafting_system = CraftingSystem.new() if crafting_system.craft(item, inventory): print("Crafted", item) else: print("Not enough resources to craft", item)

User Interface

14. Create UI for Health, Stamina, Hunger, and Thirst

Add a simple UI to display player stats:

  1. Add a CanvasLayer node to the Main node.
  2. Add Label nodes as children of the CanvasLayer to display health, stamina, hunger, and thirst.
  3. Update the UI in _process:
gdscript
func _process(delta): $CanvasLayer/HealthLabel.text = "Health: " + str(health) $CanvasLayer/StaminaLabel.text = "Stamina: " + str(stamina) $CanvasLayer/HungerLabel.text = "Hunger: " + str(hunger) $CanvasLayer/ThirstLabel.text = "Thirst: " + str(thirst)

Conclusion

By following these steps, you have created the foundation for a 3D survival game in Godot 4.2 with GDScript. You can expand upon this foundation by adding more features like advanced AI, a day-night cycle, multiplayer capabilities, and more sophisticated crafting and building systems. Godot's flexibility and powerful scripting language allow you to bring your game ideas to life efficiently. Happy game development!