3D ENDLESS RUNNER IN UNITY - COINS (Pt 4)

Practical Programming
16 Aug 202022:48

Summary

TLDRIn this tutorial, the speaker guides viewers through creating a 3D endless runner game in Unity. The video focuses on adding collectible coins that spawn at random positions on the game tiles, interact with the player to increase their score, and destroy themselves once collected. Additionally, the game score is displayed on the screen, and solutions to common gameplay issues like coins spawning inside obstacles are provided. The tutorial also covers implementing a game manager to track and display the player’s score, with practical tips for visual and gameplay enhancements. It’s a great follow-up for anyone looking to expand their Unity skills.

Takeaways

  • 😀 Create a coin prefab by adding a cylinder, adjusting its scale, and applying a yellow material.
  • 😀 Set the coin's collider as a trigger to allow interaction without altering its physics behavior.
  • 😀 Write a script to detect when the player collects a coin using the OnTriggerEnter function and add it to the player's score.
  • 😀 Rotate the coin using a public 'turnSpeed' variable in the script, allowing it to spin continuously.
  • 😀 Spawn multiple coins randomly within the bounds of a tile using the 'Instantiate' method in the GroundTile script.
  • 😀 Use a helper function to generate random spawn positions inside the tile's collider, ensuring coins spawn within accessible areas.
  • 😀 Parent spawned coins to the ground tile to automatically clean up coins when the tile is destroyed, improving performance.
  • 😀 Add a rigidbody component to the coin prefab and check if it's intersecting obstacles to prevent spawning inside them.
  • 😀 Implement a GameManager to track and manage the player's score, using a public static instance for easy access.
  • 😀 Update the score UI by linking a Text object in Unity and updating it whenever a coin is collected.
  • 😀 Use Unity's UI system to display the score at the top-left corner of the screen and make it dynamic as coins are collected.

Q & A

  • What is the purpose of the script in this video?

    -The script aims to add coins to the game in Unity, which spawn at random positions on each ground tile, contribute to the player's score when collected, and display the score on the screen.

  • How does the coin rotation work in Unity?

    -The coin rotates by using the 'transform.rotate' function in the 'Update' method, with the 'turnSpeed' variable controlling the rotation speed, set to 90 degrees per second by default.

  • Why is the 'capsule collider' used for the coin?

    -The capsule collider is set as a trigger to ensure the coin interacts with the player without changing the player's direction, allowing the coin to be collected without causing physical collisions.

  • What is the role of the 'spawn coins' function?

    -The 'spawn coins' function is responsible for instantiating a set number of coins at random positions within the bounds of the ground tile, using a random number generator for each coin's position.

  • How does the random coin spawning position function work?

    -The 'getRandomPointInCollider' function generates random positions within the bounds of a collider using the 'Random.Range' method to ensure that the coins spawn within the tile's area and not outside or in unreachable spaces.

  • What is the purpose of the 'rigidbody' component added to the coin?

    -The 'rigidbody' component is added to enable proper collision detection, ensuring that the coin is correctly handled when it interacts with other objects, such as obstacles.

  • Why are coins parented to the ground tile?

    -Coins are parented to the ground tile so that when the ground tile is destroyed, the coins are also destroyed, preventing unnecessary objects from cluttering the scene and causing performance issues.

  • What happens if the player collects a coin?

    -When the player collects a coin, the 'OnTriggerEnter' function in the coin's script checks for a collision with the player and increments the player's score. The coin is then destroyed.

  • How is the player's score displayed on the screen?

    -The player's score is displayed using a UI text element in Unity, which is updated by the 'GameManager' script every time a coin is collected. The score is reflected in real-time on the top-left corner of the screen.

  • What improvements were made to prevent coins from spawning inside obstacles?

    -A check was added to the coin script to destroy any coin that intersects with an obstacle. Additionally, a rigidbody was added to the coin to ensure proper collision handling, preventing coins from being trapped inside obstacles.

Outlines

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Mindmap

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Keywords

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Highlights

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Transcripts

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora
Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Unity TutorialGame Development3D RunnerCoin MechanicScore SystemGameplay DesignUnity ScriptingCoin SpawningPlayer InteractionGame ManagerEndless Runner
¿Necesitas un resumen en inglés?