¡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.
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”
Add Rigid Body and Box Collider components, then make sure that Colliders surround objects properly
Create a new Scripts folder, a new “Target.cs” script inside it, attach it to the Target objects
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.
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”
Add Rigid Body and Box Collider components, then make sure that Colliders surround objects properly
Create a new Scripts folder, a new “Target.cs” script inside it, attach it to the Target objects
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.
In Target.cs, declare a new private Rigidbody targetRb; and initialize it in Start()
In Start(), add an upward force multiplied by a
randomized speed
Add a torque with randomized xyz values
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.
Declare and initialize new private float variables for minSpeed, maxSpeed, maxTorque, xRange, and ySpawnPos;
Create a new function for Vector3 RandomForce() and call it in Start()
Create a new function for float RandomTorque() and call it in Start()
Create a new function for RandomSpawnPos(), have it return a new
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.
Create a new “Game Manager” Empty object, attach a new GameManager.cs script, then open it
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.
Declare and initialize a new private float spawnRate
variable
Create a new IEnumerator SpawnTarget () method
Inside the new method, while(true), wait 1 second, generate a random index, and spawn a random target
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.
In Target.cs, add a new method for private void OnMouseDown() { } , and inside that method, destroy the gameObject
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.
In the Hierarchy, Create > UI > TextMeshPro text, then if prompted click the button to Import TMP Essentials
Rename the new object “Score Text”, then zoom out to see the canvas in Scene view
Change the Anchor Point so that it is anchored from the top-left corner
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.
Change its text to “Score:”
Choose a Font Asset, Style, Size, and Vertex color
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.
At the top of GameManager.cs, add “using TMPro;”
Declare a new public TextMeshProUGUI scoreText, then assign that variable in the inspector
Create a new private int score variable and initialize it in Start() as score = 0;
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.
Create a new private void UpdateScore method that requires one int scoreToAdd parameter
Cut and paste scoreText.text = "Score: " + score; into the new method, then call UpdateScore(0) in Start()
In UpdateScore(), increment the score by adding
score += scoreToAdd;
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.
In GameManager.cs, make the UpdateScore method public
In Target.cs, create a reference to private GameManager gameManager;
Initialize GameManager in Start() using the Find() method
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.
In Target.cs, create a new public int pointValue variable
In each of the Target prefab’s inspectors, set the Point Value to whatever they’re worth, including the bad target’s negative value
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!
In Target.cs, add a new public ParticleSystem explosionParticle
variable
For each of your target prefabs, assign a particle prefab from Course Library > Particles to the Explosion Particle variable
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”.
Right-click on the Canvas, create a new UI > TextMeshPro - Text object, and rename it “Game Over Text”
In the inspector, edit its Text, Pos X, Pos Y, Font Asset, Size, Style, Color, and Alignment
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.
In GameManager.cs, create a new public TextMeshProUGUI gameOverText; and assign the Game Over object to it in the inspector
Uncheck the Active checkbox to deactivate the Game Over text by default
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.
Create a new public void GameOver() function, and move the code that activates the game over text inside it
In Target.cs, call gameManager.GameOver() if a target collides with the sensor
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.
Create a new public bool isGameActive;
As the first line In Start(), set isGameActive = true; and in GameOver(), set isGameActive = false;
To prevent spawning, in the SpawnTarget() coroutine, change while (true) to while (isGameActive)
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
Right-click on the Canvas and Create > UI > Button
Note: You could also use Button - TextMeshPro for more control over the button’s text.
Rename the button “Restart Button”
Temporarily reactivate the Game Over text in order to reposition the Restart Button nicely with the text, then deactivate it again
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.
In GameManager.cs, add using UnityEngine.SceneManagement;
Create a new public void RestartGame() function that reloads the current scene
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.
At the top of GameManager.cs add using UnityEngine.UI;
Declare a new public Button restartButton; and assign the
Restart Button to it in the inspector
Uncheck the “Active” checkbox for the Restart Button in the inspector
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.
Duplicate your Game Over text to create your Title Text, editing its name, text and all of its attributes
Duplicate your Restart Button and edit its attributes to create an “Easy Button” button
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.
For all 3 new buttons, in the Button component, in the On Click () section, click the minus (-) button to remove the RestartGame functionality
Create a new DifficultyButton.cs script and attach it to all 3 buttons
Add using UnityEngine.UI to your imports
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
Create a new void SetDifficulty function, and inside it, Debug.Log(gameObject.name + " was clicked");
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.
In GameManager.cs, create a new public void StartGame() function and move everything from Start() into it
In DifficultyButton.cs, create a new private GameManager gameManager; and initialize it in Start()
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.
Right-click on the Canvas and Create > Empty Object, rename it “Title Screen”, and drag the 3 buttons and title onto it
In GameManager.cs, create a new public GameObject titleScreen; and assign it in the inspector
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.
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
Add an int difficulty parameter to the StartGame() function
In StartGame(), set spawnRate /= difficulty;
Fix the error in DifficultyButton.cs by passing the difficulty parameter to StartGame(difficulty)
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:
View
Essential Business Proposal
View
Project Roadmap Timeline
View
Step-by-Step Timeline: How to Develop an Idea
View
Artificial Intelligence History Timeline
View
Pictures quiz
View
Big Data: The Data That Drives the World
View
Mind Map: The 4 Pillars of Success
Explore all templates
Transcript
Título 2
Título 1
Título 2
Título 2
Instrucciones
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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!
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”.
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.
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.
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.
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
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.
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.
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.
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.
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
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.
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.
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.