Circular endless runner featuring double jump built with Unity and C# using only trigonometry

Read all posts about "" game

The was some engagement about the post “HTML5 prototype of a circular endless runner featuring double jump built with Phaser using only trigonometry” so I decided to do the same thing using Unity.

Again, no physics engines have been used, only trigonometry, and the result is obviously identical:

Click or tap to jump and double jump.

It was very easy to build, if you are familiar with Unity this is the main screen with all assets and prefabs shown, as well as the global variables used in the script.

And this is the script, a very few lines like in the HTML5 prototype of a circular endless runner featuring double jump built with Phaser using only trigonometryPhaser version.

I also made some changes to game logic, and now speed is measured in “seconds to make a complete revolution”. It works better when dealing with the delta time from a frame to next frame.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mainScript : MonoBehaviour {

	public GameObject player;
	public float bigCircleRadius;
	public float smallCircleRadius;
	public float revolutionSpeed;
	public float currentAngle;
	private float revolutions;
	public float gravity;
	public float[] jumpForce;
	private float jumpOffset = 0;
	private int jumps = 0;
	private float currentJumpForce = 0;

	void Start () {
		revolutions = bigCircleRadius / smallCircleRadius + 1;	
	}

	void Update () {
		handleJumps ();
		handleRevolution (Time.deltaTime);
	}

	void handleJumps(){
		if (Input.GetButtonDown ("Fire1")) {
			if (jumps < 2) {
				jumps++;
				currentJumpForce = jumpForce [jumps - 1];
			}	
		}
		if (jumps > 0) {
			jumpOffset += currentJumpForce;
			currentJumpForce -= gravity;
			if (jumpOffset < 0) {
				jumpOffset = 0;
				jumps = 0;
				currentJumpForce = 0;
			}
		}
	}

	void handleRevolution(float elapsedTime){
		currentAngle += 360f * elapsedTime / revolutionSpeed;
		Vector2 newPosition = player.transform.position;
		float radians = currentAngle * Mathf.Deg2Rad;
		float totalRadius = bigCircleRadius + smallCircleRadius + jumpOffset;
		newPosition.x = totalRadius * Mathf.Cos (radians);
		newPosition.y = totalRadius * Mathf.Sin (radians);
		player.transform.position = newPosition;
		player.transform.Rotate (0, 0, 360f * revolutions * elapsedTime / revolutionSpeed);
	}

}

I think it’s time for a tutorial series using Unity, meanwhile download the source code and tell me how would you expand the prototype.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers