3D Survival Game Tutorial | Unity | Part 1: Getting Started & Player Movement

3 min read 2 hours ago
Published on Oct 19, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will explore the fundamental aspects of creating a 3D survival game using Unity and C#. This first part focuses on setting up your project and implementing basic player movement, which is essential for any game. By the end of this tutorial, you will have a solid foundation to build upon as you expand your game’s features.

Step 1: Setting Up Your Unity Project

  1. Install Unity: Download and install the latest version of Unity Hub and Unity Editor from the Unity official website.
  2. Create a New Project:
    • Open Unity Hub.
    • Click on "New Project."
    • Select the "3D" template.
    • Name your project (e.g., "3DSurvivalGame") and choose a location for it.
    • Click "Create."
  3. Set Up the Scene:
    • In the Hierarchy panel, right-click and select "3D Object" > "Plane" to create the ground.
    • Right-click again, select "3D Object" > "Cube," and place it above the plane as your player object.
    • Adjust the scale and position as needed.

Step 2: Implementing Player Movement

  1. Create Player Movement Script:

    • In the Project panel, right-click in the Assets folder and select "Create" > "C# Script."
    • Name it "PlayerMovement."
    • Double-click the script to open it in your code editor.
  2. Add Movement Code:

    • Use the following code snippet to enable basic player movement:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PlayerMovement : MonoBehaviour
    {
        public float moveSpeed = 5f;
    
        void Update()
        {
            MovePlayer();
        }
    
        void MovePlayer()
        {
            float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
            float vertical = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
    
            transform.Translate(horizontal, 0, vertical);
        }
    }
    
  3. Attach the Script to the Player Object:

    • Select your Cube (player object) in the Hierarchy.
    • In the Inspector panel, click "Add Component."
    • Search for "PlayerMovement" and add it.

Step 3: Testing Player Movement

  1. Enter Play Mode:
    • Click the "Play" button at the top of the Unity editor.
    • Use the "WASD" keys or arrow keys to move the player object around the plane.
  2. Adjust Movement Speed:
    • If the movement is too slow or fast, adjust the moveSpeed variable in the PlayerMovement script through the Inspector.

Step 4: Implementing Mouse Movement (Optional)

  1. Create Mouse Movement Script:

    • Create another C# script named "MouseMovement."
    • Use the following code snippet for mouse rotation:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MouseMovement : MonoBehaviour
    {
        public float sensitivity = 100f;
    
        void Update()
        {
            float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
            transform.Rotate(Vector3.up * mouseX);
        }
    }
    
  2. Attach the Mouse Movement Script:

    • Attach the "MouseMovement" script to the player object (Cube).

Conclusion

In this tutorial, you learned how to set up a Unity project and implement basic player movement for your 3D survival game. You now have a player that can move around the environment using keyboard inputs. In the next parts of this series, we will expand on this foundation by adding inventory systems, crafting mechanics, and interaction with items.

Feel free to explore and experiment with Unity further to enhance your game. Happy developing!