Want to create interactive content? It’s easy in Genially!

Get started free

Quinto proyecto en Unity 3D

ati.innovacion

Created on June 16, 2026

Start designing with a free template

Discover more than 1500 professional designs like these:

Essential Business Proposal

Project Roadmap Timeline

Step-by-Step Timeline: How to Develop an Idea

Artificial Intelligence History Timeline

Polka Dots Presentation

Carrera de estrellas PRUEBA

Pictures quiz

Transcript

Título 2

Título 1

Título 2

Título 2

Instrucciones

  • ¡Listo para utilizar el Mouse! Ahora desarrollarás tu quinto proyecto paso a paso.
  • Encontrarás instrucciones individuales y ejemplos de código en las diapositivas para apoyarte en el proceso.
  • ¡Mucho éxito en esta quinta aventura con Unity!
  • Nota: Recuerda crear un proyecto nuevo, descargar el paquete “03_Prototype+5” e importarlo a tu proyecto (En caso de no recordar este proceso revisa los pasos del proyecto 1).

Presentación del proyecto

Presentación del proyecto

Step 1: Create good and bad targetsThe first thing we need in our game are three good objects to collect, and one bad object to avoid. It’ll be up to you to decide what’s good and what’s bad.

  1. From the Library, drag 3 “good” objects and 1 “bad” object into the Scene, rename them “Good 1”, “Good 2”, “Good 3”, and “Bad 1”
  2. Add Rigid Body and Box Collider components, then make sure that Colliders surround objects properly
  3. Create a new Scripts folder, a new “Target.cs” script inside it, attach it to the Target objects
  4. Drag all 4 targets into the Prefabs folder to create “original prefabs”, then delete them from the scene

Step 2: Toss objects randomly in the airNow that we have 4 target prefabs with the same script, we need to toss them into the air with a random force, torque, and position.

  1. From the Library, drag 3 “good” objects and 1 “bad” object into the Scene, rename them “Good 1”, “Good 2”, “Good 3”, and “Bad 1”
  2. Add Rigid Body and Box Collider components, then make sure that Colliders surround objects properly
  3. Create a new Scripts folder, a new “Target.cs” script inside it, attach it to the Target objects
  4. Drag all 4 targets into the Prefabs folder to create “original prefabs”, then delete them from the scene

Step 3: Toss objects randomly in the airNow that we have 4 target prefabs with the same script, we need to toss them into the air with a random force, torque, and position.

  1. In Target.cs, declare a new private Rigidbody targetRb; and initialize it in Start()
  2. In Start(), add an upward force multiplied by a
  3. randomized speed
  4. Add a torque with randomized xyz values
  5. Set the position with a randomized X value

Step 4: Replace messy code with new methodsInstead of leaving the random force, torque, and position making our Start() function messy and unreadable, we’re going to store each of them in brand new clearly named custom methods.

  1. Declare and initialize new private float variables for minSpeed, maxSpeed, maxTorque, xRange, and ySpawnPos;
  2. Create a new function for Vector3 RandomForce() and call it in Start()
  3. Create a new function for float RandomTorque() and call it in Start()
  4. Create a new function for RandomSpawnPos(), have it return a new
  5. Vector3 and call it in Start()

Code

Step 5: Create object list in Game ManagerThe next thing we should do is create a list for these objects to spawn from. Instead of making a Spawn Manager for these spawn functions, we’re going to make a Game Manager that will also control game states later on.

  1. Create a new “Game Manager” Empty object, attach a new GameManager.cs script, then open it
  2. Declare a new public List targets;, then in the Game Manager inspector, change the list Size to 4 and assign your prefabs

Step 6: Create a coroutine to spawn objectsNow that we have a list of object prefabs, we should instantiate them in the game using coroutines and a new type of loop.

  1. Declare and initialize a new private float spawnRate
  2. variable
  3. Create a new IEnumerator SpawnTarget () method
  4. Inside the new method, while(true), wait 1 second, generate a random index, and spawn a random target
  5. In Start(), use the StartCoroutine method to begin spawning objects

Step 7: Destroy target with click and sensorNow that our targets are spawning and getting tossed into the air, we need a way for the player to destroy them with a click. We also need to destroy any targets that fall below the screen.

  1. In Target.cs, add a new method for private void OnMouseDown() { } , and inside that method, destroy the gameObject
  2. Add a new method for private void OnTriggerEnter(Collider other) and inside that function, destroy the gameObject

5.2 Keeping Score

Title 1

Step 1: Add Score text position it on screenIn order to display the score on-screen, we need to add our very first UI element.

  1. In the Hierarchy, Create > UI > TextMeshPro text, then if prompted click the button to Import TMP Essentials
  2. Rename the new object “Score Text”, then zoom out to see the canvas in Scene view
  3. Change the Anchor Point so that it is anchored from the top-left corner
  4. In the inspector, change its Pos X and Pos Y so that it is in the top-left corner

Step 2: Edit the Score Text’s propertiesNow that the basic text is in the scene and positioned properly, we should edit its properties so that it looks nice and has the correct text.

  1. Change its text to “Score:”
  2. Choose a Font Asset, Style, Size, and Vertex color
  3. to look good with your background

Step 3: Initialize score text and variableWe have a great place to display score in the UI, but nothing is displaying there! We need the UI to display a score variable, so the player can keep track of their points.

  1. At the top of GameManager.cs, add “using TMPro;”
  2. Declare a new public TextMeshProUGUI scoreText, then assign that variable in the inspector
  3. Create a new private int score variable and initialize it in Start() as score = 0;
  4. Also in Start(), set scoreText.text = "Score: " + score;

Step 4: Create a new UpdateScore methodThe score text displays the score variable perfectly, but it never gets updated. We need to write a new function that racks up points to display in the UI.

  1. Create a new private void UpdateScore method that requires one int scoreToAdd parameter
  2. Cut and paste scoreText.text = "Score: " + score; into the new method, then call UpdateScore(0) in Start()
  3. In UpdateScore(), increment the score by adding
  4. score += scoreToAdd;
  5. Call UpdateScore(5) in the spawnTarget() function

Step 5: Add score when targets are destroyedNow that we have a method to update the score, we should call it in the target script whenever a target is destroyed.

  1. In GameManager.cs, make the UpdateScore method public
  2. In Target.cs, create a reference to private GameManager gameManager;
  3. Initialize GameManager in Start() using the Find() method
  4. When a target is destroyed, call UpdateScore(5);, then delete the method call from SpawnTarget()

Code

Step 6: Assign a point value to each targetThe score gets updated when targets are clicked, but we want to give each of the targets a different value. The good objects should vary in point value, and the bad object should subtract points.

  1. In Target.cs, create a new public int pointValue variable
  2. In each of the Target prefab’s inspectors, set the Point Value to whatever they’re worth, including the bad target’s negative value
  3. Add the new variable to UpdateScore(pointValue);

Step 7: Add a Particle explosionThe score is totally functional, but clicking targets is sort of… unsatisfying. To spice things up, let’s add some explosive particles whenever a target gets clicked!

  1. In Target.cs, add a new public ParticleSystem explosionParticle
  2. variable
  3. For each of your target prefabs, assign a particle prefab from Course Library > Particles to the Explosion Particle variable
  4. In the OnMouseDown() function, instantiate a new explosion prefab

5.3 Game Over

5.3 Game Over

Step 1: Create a Game Over text objectIf we want some “Game Over” text to appear when the game ends, the first thing we’ll do is create and customize a new UI text element that says “Game Over”.

  1. Right-click on the Canvas, create a new UI > TextMeshPro - Text object, and rename it “Game Over Text”
  2. In the inspector, edit its Text, Pos X, Pos Y, Font Asset, Size, Style, Color, and Alignment
  3. Set the “Wrapping” setting to “Disabled”

Step 2: Make GameOver text appearWe’ve got some beautiful Game Over text on the screen, but it’s just sitting and blocking our view right now. We should deactivate it, so it can reappear when the game ends.

  1. In GameManager.cs, create a new public TextMeshProUGUI gameOverText; and assign the Game Over object to it in the inspector
  2. Uncheck the Active checkbox to deactivate the Game Over text by default
  3. In Start(), activate the Game Over text

Step 3: Create GameOver functionWe’ve temporarily made the “Game Over” text appear at the start of the game, but we actually want to trigger it when one of the “Good” objects is missed and falls.

  1. Create a new public void GameOver() function, and move the code that activates the game over text inside it
  2. In Target.cs, call gameManager.GameOver() if a target collides with the sensor
  3. Add a new “Bad” tag to the Bad object, add a condition that will only trigger game over if it’s not a bad object

Step 4: Stop spawning and score on GameOverThe “Game Over” message appears exactly when we want it to, but the game itself continues to play. In order to truly halt the game and call this a “Game Over’, we need to stop spawning targets and stop generating score for the player.

  1. Create a new public bool isGameActive;
  2. As the first line In Start(), set isGameActive = true; and in GameOver(), set isGameActive = false;
  3. To prevent spawning, in the SpawnTarget() coroutine, change while (true) to while (isGameActive)
  4. To prevent scoring, in Target.cs, in the OnMouseDown() function, add the condition if (gameManager.isGameActive)

Step 5: Add a Restart buttonOur Game Over mechanics are working like a charm, but there’s no way to replay the game. In order to let the player restart the game, we will create our first UI button

  1. Right-click on the Canvas and Create > UI > Button
  2. Note: You could also use Button - TextMeshPro for more control over the button’s text.
  3. Rename the button “Restart Button”
  4. Temporarily reactivate the Game Over text in order to reposition the Restart Button nicely with the text, then deactivate it again
  5. Select the Text child object, then edit its Text to say “Restart”, its Font, Style, and Size

Step 6: Make the restart button workWe’ve added the Restart button to the scene and it LOOKS good, but now we need to make it actually work and restart the game.

  1. In GameManager.cs, add using UnityEngine.SceneManagement;
  2. Create a new public void RestartGame() function that reloads the current scene
  3. In the Button’s inspector, click + to add a new On Click event, drag it in the Game Manager object and select the GameManager.RestartGame function

Step 7: Show restart button on game overThe Restart Button looks great, but we don’t want it in our faces throughout the entire game. Similar to the “Game Over” message, we will turn off the Restart Button while the game is active.

  1. At the top of GameManager.cs add using UnityEngine.UI;
  2. Declare a new public Button restartButton; and assign the
  3. Restart Button to it in the inspector
  4. Uncheck the “Active” checkbox for the Restart Button in the inspector
  5. In the GameOver function, activate the Restart Button

    5.4 What’s the Difficulty?

    5.4 What’s the Difficulty?

    Step 1: Create Title text and menu buttonsThe first thing we should do is create all of the UI elements we’re going to need. This includes a big title, as well as three dificulty buttons.

    1. Duplicate your Game Over text to create your Title Text, editing its name, text and all of its attributes
    2. Duplicate your Restart Button and edit its attributes to create an “Easy Button” button
    3. Edit and duplicate the new Easy button to create a“Medium Button” and a “Hard Button”

    Step 2: Add a DifficultyButton scriptOur dificulty buttons look great, but they don’t actually do anything. If they’re going to have custom functionality, we first need to give them a new script.

    1. For all 3 new buttons, in the Button component, in the On Click () section, click the minus (-) button to remove the RestartGame functionality
    2. Create a new DifficultyButton.cs script and attach it to all 3 buttons
    3. Add using UnityEngine.UI to your imports
    4. Create a new private Button button; variable and initialize it in Start()

    Step 3: Call SetDifficulty on button clickNow that we have a script for our buttons, we can create a SetDificulty method and tie that method to the click of those buttons

    1. Create a new void SetDifficulty function, and inside it, Debug.Log(gameObject.name + " was clicked");
    2. Add the button listener to call the SetDifficulty function

    Step 4: Make your buttons start the gameThe Title Screen looks great if you ignore the target objects bouncing around, but we have no way of actually starting the game. We need a StartGame function that can communicate with SetDificulty.

    1. In GameManager.cs, create a new public void StartGame() function and move everything from Start() into it
    2. In DifficultyButton.cs, create a new private GameManager gameManager; and initialize it in Start()
    3. In the SetDifficulty() function, call gameManager.startGame();

    Step 5: Deactivate Title Screen on StartGameIf we want the title screen to disappear when the game starts, we should store them in an empty object rather than turning them off individually. Simply deactivating the single empty parent object makes for a lot less work.

    1. Right-click on the Canvas and Create > Empty Object, rename it “Title Screen”, and drag the 3 buttons and title onto it
    2. In GameManager.cs, create a new public GameObject titleScreen; and assign it in the inspector
    3. In StartGame(), deactivate the title screen object

    Step 6: Use a parameter to change difficultyThe dificulty buttons start the game, but they still don’t change the game’s dificulty. The last thing we have to do is actually make the dificulty buttons affect the rate that target objects spawn.

    1. In DifficultyButton.cs, create a new public int difficulty variable, then in the Inspector, assign the Easy difficulty as 1, Medium as 2, and Hard as 3
    2. Add an int difficulty parameter to the StartGame() function
    3. In StartGame(), set spawnRate /= difficulty;
    4. Fix the error in DifficultyButton.cs by passing the difficulty parameter to StartGame(difficulty)