Skip to content

Instantly share code, notes, and snippets.

@silver-hornet
silver-hornet / Trajectory Indicator for Projectiles
Last active July 27, 2022 12:33
Trajectory Indicator for Projectiles
// 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;
@silver-hornet
silver-hornet / Ticker
Last active July 23, 2022 23:37
Ticker
// 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;
@silver-hornet
silver-hornet / Coin Manager (using PlayerPrefs)
Created June 13, 2021 17:08
Coin Manager (using PlayerPrefs)
// 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
@silver-hornet
silver-hornet / 2D Top-Down Character Movement (grid based movement, with RigidBody2D)
Last active May 29, 2021 16:00
2D Top-Down Character Movement (grid based movement, with RigidBody2D)
// 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;
@silver-hornet
silver-hornet / Object Spawner
Last active July 18, 2022 00:21
Object Spawner
// 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;
@silver-hornet
silver-hornet / 2D Moving Object (with RigidBody2D)
Last active May 29, 2021 15:46
2D Moving Object (with RigidBody2D)
// 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;
@silver-hornet
silver-hornet / 2D Bouncing Ball (with Rigidbody2D)
Last active February 22, 2021 16:14
2D Bouncing Ball (with Rigidbody2D)
// 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);
@silver-hornet
silver-hornet / 2D Character Movement (with RigidBody2D)
Last active February 22, 2021 16:11
2D Character Movement (with RigidBody2D)
// 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;
@silver-hornet
silver-hornet / Set Screen Boundaries for 2D Player
Last active July 23, 2022 23:32
Set Screen Boundaries for 2D Player
// 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;
@silver-hornet
silver-hornet / 2D Top-Down Character Movement (grid based movement, without RigidBody2D)
Last active September 28, 2023 21:15
2D Top-Down Character Movement (grid based movement, without RigidBody2D)
// 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
{