Talking about Circular endless runner game, C#, Game development and Unity3D.
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.