This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from tutorial: https://www.youtube.com/watch?v=Qxs3GrhcZI8 (which also shows how to convert this to 3D as well) | |
// 1. Create a blank object (called Projectile) with a basic Sprite Renderer | |
// 2. Create a basic Material in the Project hierarchy | |
// 3. Create a blank object (called Line) with a basic Line Renderer attached | |
// 4. Create a blank object (called Player) withh a basic Sprite Renderer and 2D collider | |
// 4. Attach this script to the Projectile game object | |
// 5. In the Inspector, set Initial Velocity to 5, Angle to 45, drag the Line game object into Line, set Step to 0.1, and drag the Player game object into Fire Point | |
// 6. Make sure you're using Unity's new Input System | |
using System.Collections; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script is adapted from a YouTube tutorial: https://www.youtube.com/watch?v=mptVj9-I0gQ | |
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Ticker : MonoBehaviour | |
{ | |
public static Ticker instance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapt this script for any game where you need coin management (eg. where winning or losing a round of something affects your coin balance). | |
// Create a Coin Manager game object and add this script to it. | |
// Replace SimulateGame() with the actual functionality your game needs to get to a result that is then handled by GetResult(). | |
// This script relies on Unity's built-in PlayerPrefs system. This allows data to be saved and automatically retrieved during subsequent game sessions. | |
// Note: PlayerPrefs, while convenient, isn't very secure (a savvy user will know where to find the saved data on their system, and will be able to modify the values to "cheat"). | |
// So it's important to understand the limitations of PlayerPrefs and the scenarios in which it makes sense to implement. | |
using UnityEngine; | |
public class CoinManager : MonoBehaviour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script enables simple, 2D top-down grid based character movement. | |
// Ensure you have added a RigidBody2D component in the Inspector and have set its Body Type to Kinematic. | |
// The player only moves in units of 1 on the x or y axis and is unable to move diagonally or continuously. | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour | |
{ | |
[SerializeField] Rigidbody2D rb; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script will spawn an instance of a prefab from a random spawn position. | |
// Create an ObjectSpawner game object and add this script to it. | |
// Create one or more child game objects and set each transform to the position you'd like to spawn objects from. | |
using UnityEngine; | |
public class ObjectSpawner : MonoBehaviour | |
{ | |
[SerializeField] float spawnDelay = 0.3f; | |
[SerializeField] GameObject objectPrefab; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add this script to any 2D object you would like to have move automatically once it has been spawned from a different script. | |
// If you're spawning this game object from another script, you'll want this game object to be a prefab. | |
// Ensure you have added a RigidBody2D component in the Inspector and have set its Body Type to Kinematic. | |
// Add the relevant 2D collider (such as a a Box Collider 2D) in the Inspector. | |
using UnityEngine; | |
public class MovingObject : MonoBehaviour | |
{ | |
[SerializeField] Rigidbody2D rb; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script creates a 2D bouncing ball. | |
// Ensure you have added a RigidBody2D component in the Inspector and have set its Body Type to Dynamic. | |
// Use a circle-shaped sprite and a Circle Collider 2D in the Inspector. | |
// Create a Physic Material with a Bounciness value set to 1, and attach it to the Circle Collider 2D. | |
using UnityEngine; | |
public class BouncingBall : MonoBehaviour | |
{ | |
[SerializeField] Vector2 startForce = new Vector2(2, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script enables simple, horizontal 2D character movement with a RigidBody2D. | |
// Ensure you have added a RigidBody2D component in the Inspector and have set its Body Type to Dynamic. | |
// GetAxisRaw makes the movement snappy, so the player instantly stops. If you want smoother movement, use GetAxis instead. | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour | |
{ | |
[SerializeField] float speed = 4f; | |
Rigidbody2D rb; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script ensures that the player will not be able to move outside the ranges set by the min and max floats on the x and y axis. | |
// If you need your screen boundaries to be symetrical, you could have less code below by replacing the four variables with | |
// only two instead (eg. boundaryX, boundaryY). Then in the Mathf.Clamp statements, | |
// you would simply use (transform.position.x, -boundaryX, boundaryX) and (transform.position.y, -boundaryY, boundaryY) | |
using UnityEngine; | |
public class ScreenBoundaries : MonoBehaviour | |
{ | |
float minX = -9f; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This script enables simple, 2D top-down grid based character movement without a RigidBody2D. | |
// Set the speed in the Inspector. | |
// The player only moves in units of 1 on the x or y axis and is unable to move diagonally. | |
// GetAxisRaw makes the movement snappy, so the player instantly stops. If you want smoother movement, use GetAxis instead. | |
// If you don't multiply by Time.deltaTime, you will need to use a much smaller float for moveSpeed. | |
using UnityEngine; | |
public class PlayerMovement : MonoBehaviour | |
{ |
NewerOlder