Gameplay.LavaLauncher - robblofield/TomeboundDocs GitHub Wiki
Gameplay.LavaLauncher
Author: Joel O'Halloran
Last Updated: 03/01/2024
Overview
- A script that launches a lava enemy out of the ground from one tile to another using the slerp function.
Dependencies
N/A
Contents
- Use case
- Breakdown of code
- Using Statements
- Class Declaration
- Variables
- Method/Start()
- Method/Update()
- Method/ArcLoop()
- Method/StartEnemy()
- Future Expansion
- Full Code Reference
Use case
This is currently being used to add another element of difficulty to a level by giving the player an extra enemy to pass without taking damage.
Breakdown of Code
Using Statements
// Paste in your "Using" statements.
using System.Collections;
using UnityEngine;
Class Declaration
public class LavaLauncher : MonoBehaviour
Variables
public Transform jumpStart;
public Transform jumpEnd;
public float dist;
public GameObject enemy;
private GameObject currentInstance;
public float journeyTime = 1.0f;
public float respawnTime;
private float startTime;
public float length;
public Transform jumpStart:
This is the start point of the lava enemy's jump.
public Transform jumpEnd:
This is the end point of the lava enemy's jump.
public float dist:
Measures the distance between the lava enemy's current position and jumpEnd.
public GameObject enemy:
Represents the GameObject of the lava enemy.
private GameObject currentInstance:
Represents the GameObject assigned to the enemy's currentInstance;
public float journeyTime = 1.0f:
This variable defines the time it takes for the lava enemy to complete one jump from the start point to the end point.
public float respawnTime:
This variable defines the amount of time it takes for the lava enemy to respawn after it reaches the end point.
private float startTime:
This variable defines the start time of the jump.
Start()
private void Start()
{
StartEnemy();
}
This method calls the StartEnemy function.
Update()
void Update()
{
if (currentInstance != null)
{
Vector3 center = (jumpStart.position + jumpEnd.position) * 0.5F;
center -= new Vector3(0, 1, 0);
Vector3 riseRelCenter = jumpStart.position - center;
Vector3 setRelCenter = jumpEnd.position - center;
length = Vector3.Distance(jumpStart.transform.position, jumpEnd.transform.position);
float fracComplete = (Time.time - startTime) / journeyTime;
currentInstance.transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
currentInstance.transform.position += center;
dist = Vector3.Distance(currentInstance.transform.position, jumpEnd.position);
if (dist <= 0.1f)
{
Destroy(currentInstance);
dist = 1;
StartCoroutine(ArcLoop());
}
}
}
The update function carries out one slerp (spherical interpolation) of the lava enemy from the start point to the end point. A centre point is created for the movement and the lava enemy interpolates relative to that point. The distance is also found between the two points in the current instance. If this is less than a certain value, it will destroy the current instance and start the coroutine named ArcLoop.
ArcLoop()
IEnumerator ArcLoop()
{
yield return new WaitForSeconds(respawnTime);
StartEnemy();
}
This coroutine controls the respawn time after the lava enemy has carried out one interpolation between the start and end points. The StartEnemy function is also called.
StartEnemy()
void StartEnemy()
{
currentInstance = Instantiate(enemy, jumpStart.position, jumpStart.rotation);
startTime = Time.time;
}
This function instantiates the enemy in the current instance using the start position and end position. the startTime variable is also assigned the value of Time.time.
Future Expansion
N/A
Full Code Reference
Paste the full script below within the code block area.
using System.Collections;
using UnityEngine;
public class LavaLauncher : MonoBehaviour
{
public Transform jumpStart;
public Transform jumpEnd;
public float dist;
public GameObject enemy;
private GameObject currentInstance;
public float journeyTime = 1.0f;
public float respawnTime;
private float startTime;
public float length;
void Start()
{
StartEnemy();
}
void Update()
{
if (currentInstance != null)
{
Vector3 center = (jumpStart.position + jumpEnd.position) * 0.5F;
center -= new Vector3(0, 1, 0);
Vector3 riseRelCenter = jumpStart.position - center;
Vector3 setRelCenter = jumpEnd.position - center;
length = Vector3.Distance(jumpStart.transform.position, jumpEnd.transform.position);
float fracComplete = (Time.time - startTime) / journeyTime;
currentInstance.transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
currentInstance.transform.position += center;
dist = Vector3.Distance(currentInstance.transform.position, jumpEnd.position);
if (dist <= 0.1f)
{
Destroy(currentInstance);
dist = 1;
StartCoroutine(ArcLoop());
}
}
}
IEnumerator ArcLoop()
{
yield return new WaitForSeconds(respawnTime);
StartEnemy();
}
void StartEnemy()
{
currentInstance = Instantiate(enemy, jumpStart.position, jumpStart.rotation);
startTime = Time.time;
}
}