[TUTO] Les BASES de C# pour Unity en français : tuto débutant !

4 min read 4 hours ago
Published on Sep 24, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to introduce beginners to the basics of C# programming specifically for creating video games in Unity. Whether you're completely new to programming or looking to expand your skills, this guide will provide you with foundational knowledge to start developing games in Unity.

Step 1: Set Up Your Project

  • Open Unity Hub and create a new project.
  • Choose a template (2D or 3D) based on your game's needs.
  • Name your project and select a save location.
  • Click "Create" to initialize your project.

Step 2: Create a Script

  • In the Unity editor, navigate to the "Assets" folder in the Project panel.
  • Right-click in the folder and select "Create" > "C# Script."
  • Name your script appropriately (e.g., "PlayerController").
  • Double-click the script to open it in Visual Studio Code.

Step 3: Understand the Basic Structure of a C# Script

  • Every C# script starts with the class declaration:
    public class YourClassName : MonoBehaviour
    
  • The MonoBehaviour inheritance allows your script to interact with Unity’s engine.
  • Inside the class, you will have methods like Start() and Update():
    void Start() {
        // Initialization code here
    }
    
    void Update() {
        // Code that runs every frame
    }
    

Step 4: Write Your First Instructions

  • Start with a simple instruction to print to the console:
    void Start() {
        Debug.Log("Hello, Unity!");
    }
    
  • Save the script and return to Unity. Your message will appear in the console when you run the game.

Step 5: Explore Data Types

  • Understand the various data types in C#:
    • int for integers
    • float for floating-point numbers
    • bool for true/false values
    • string for text
  • Example of declaring variables:
    int score = 0;
    float playerSpeed = 5.0f;
    bool isGameOver = false;
    string playerName = "Player1";
    

Step 6: Working with Variables

  • Declare and initialize variables within your script.
  • Use variables to store player states, scores, and other game data.
  • Example:
    public class PlayerController : MonoBehaviour {
        int health = 100;
    
        void TakeDamage(int damage) {
            health -= damage;
        }
    }
    

Step 7: Understand Variable Scope

  • Learn about variable scope (local vs. global):
    • Local variables are declared inside methods and cannot be accessed outside.
    • Global variables are declared at the class level and can be accessed anywhere in the class.
  • Example of scope:
    public class Example {
        int globalVar = 10; // Global variable
    
        void ExampleMethod() {
            int localVar = 5; // Local variable
        }
    }
    

Step 8: Create Your First Program

  • Combine your knowledge of variables and methods to create a simple game mechanic.
  • For example, let’s create a function to increase the score:
    void IncreaseScore(int points) {
        score += points;
        Debug.Log("Score: " + score);
    }
    

Step 9: Implement Conditions

  • Use conditional statements to control game flow:
    • If statements can determine actions based on conditions:
    if (health <= 0) {
        Debug.Log("Game Over");
    }
    

Step 10: Define Functions

  • Create reusable functions to organize your code better:
    void Jump() {
        // Code for jumping
    }
    

Step 11: Manipulate Game Objects

  • Learn to manipulate game objects through scripts:
    • Move a game object:
    transform.position += new Vector3(0, playerSpeed * Time.deltaTime, 0);
    
  • Access and modify components, like Rigidbody or Collider, to add physics interactions.

Conclusion

In this tutorial, you have learned the foundational aspects of C# programming for Unity. You set up a project, created and structured scripts, and explored important programming concepts like variables, conditions, and functions. Now that you have a basic understanding, consider diving deeper into Unity's documentation and building more complex game mechanics. Happy coding!