Use WASD to move objects, and spacebar to toggle which objects are active.
Character/Environment controller
Level transitions
Audio implementation
Environmental mechanic implementation
Moving platforms
Gears
Mallet
Fans
Bomb UI implementation
Bomb and hero implementation
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Events;
public class EnvironmentController : MonoBehaviour
{
public EnvironmentControls envControls;
InputAction directionalInput;
InputAction toggleInput;
Vector2 moveMagnitude;
// Start is called before the first frame update
void Start()
{
}
void Awake()
{
envControls = new EnvironmentControls();
}
private void OnEnable()
{
directionalInput = envControls.InGame.MovePlatforms;
directionalInput.Enable();
toggleInput = envControls.InGame.Toggle;
toggleInput.Enable();
toggleInput.performed += ToggleOnOff;
}
private void OnDisable()
{
directionalInput.Disable();
}
// Update is called once per frame
void Update()
{
}
void FixedUpdate()
{
moveMagnitude = directionalInput.ReadValue<Vector2>();
if (moveMagnitude.x != 0f || moveMagnitude.y != 0f)
{
BroadcastMessage("MoveMachine", moveMagnitude);
}
}
void ToggleOnOff(InputAction.CallbackContext context)
{
BroadcastMessage("MachineToggleOnOff", true);
}
}
Script that receives messages from the environment controller, and moves the platform along a pre-determined track. These tracks were implemented, because the platforms needed to be kinematic so the bomb wouldn't push them around. In the future, I would make the tracks collision areas on a track object for easier placement and development.
The trackStart lists hold coordinates for where a track starts that travels along the x or y axis respectively, and the trackEnd list is where the track ends. The lists trackStart and trackEnd will have the same number of entries for each axis.
using UnityEngine;
using UnityEngine.Events;
public class TrackPlatformScript : PlatformScript
{
[SerializeField] protected UnityEvent StartSFX;
[SerializeField] protected UnityEvent StopSFX;
public List<Vector2> trackStartX;
public List<Vector2> trackEndX;
public List<Vector2> trackStartY;
public List<Vector2> trackEndY;
private void FixedUpdate() {
Rigidbody2D rb = gameObject.GetComponent<Rigidbody2D>();
if(rb.IsAwake())
{
StartSFX.Invoke();
} else
{
StopSFX.Invoke();
}
}
bool MoveLeftRight(Vector3 movement)
{
for (int i=0; i<trackEndX.Count; i++)
{
if (gameObject.transform.position.y == trackEndX[i].y)
{
Vector3 destination = new Vector3(
(movement.x * moveSpeedMod) + gameObject.transform.position.x,
trackEndX[i].y
);
if(destination.x >= trackStartX[i].x && destination.x <= trackEndX[i].x) {
GetComponent<Rigidbody2D>().MovePosition(destination);
return true;
} else if (destination.x < trackStartX[i].x)
{
GetComponent<Rigidbody2D>().MovePosition(trackStartX[i]);
return true;
} else if (destination.x > trackEndX[i].x)
{
GetComponent<Rigidbody2D>().MovePosition(trackEndX[i]);
return true;
}
}
}
return false;
}
bool MoveUpDown(Vector3 movement)
{
for (int i=0; i<trackEndY.Count; i++)
{
if (gameObject.transform.position.x == trackEndY[i].x)
{
Vector3 destination = new Vector3(
trackEndY[i].x,
(movement.y * moveSpeedMod) + gameObject.transform.position.y
);
if(destination.y >= trackStartY[i].y && destination.y <= trackEndY[i].y) {
GetComponent<Rigidbody2D>().MovePosition(destination);
return true;
} else if (destination.y < trackStartY[i].y)
{
GetComponent<Rigidbody2D>().MovePosition(trackStartY[i]);
return true;
} else if (destination.y > trackEndY[i].y)
{
GetComponent<Rigidbody2D>().MovePosition(trackEndY[i]);
return true;
}
}
}
return false;
}
public virtual void MoveMachine(Vector2 input)
{
Vector3 movement = new Vector3(
leftRight ? input.x : 0f,
upDown ? input.y : 0f,
0f
);
if (upDown && leftRight)
{
if(input.x != 0f)
{
if(MoveLeftRight(movement)) return;
}
if(input.y != 0f)
{
if(MoveUpDown(movement)) return;
}
} else if (upDown)
{
if(MoveUpDown(movement)) return;
} else if (leftRight)
{
if(MoveLeftRight(movement)) return;
}
}
}