Doors, Doors, Doors
- Graham Kidd
- Mar 8, 2021
- 2 min read
Updated: May 18, 2021
While it is still midterms week, and I do still have tests coming up, I felt like I needed to get something done with regards to the game since it's been sitting unattended for a little while now. So I whipped together a simple door script handler and figured out how projectiles and collision triggers actually work, which is "surprisingly simple" — go watch Ceave if you haven't before, one of my favorite channels on YouTube.
No affiliation by the way, I just like their content.
That being said, it really was quite simple. I was already getting and printing the gameObject parent from objects my projectiles were colliding with, so I just used that reference and called SendMessage on it. That in turn called a method found on gameObjects like Doors, and voila! Doors receive a hit notification, are flagged to permanently unlocked if hit with the correct damage type (including charge variants), and open.
I still have some work to do, mainly
getting the door to smoothly move up
changing the door's appearance when the permanent_unlock_flag is true
getting the door to smoothly move down after a set time
making sure player collision works at all stages of the door opening/closing
making sure the player doesn't get shoved into the void when the door closes & player is under doorframe - maybe push player away or have a zone where the player's presence prevents door from closing? there are options
Anyway, here's the WIP code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Scripts
{
public class Interactables_Door : MonoBehaviour
{
[SerializeField]
private float VerticalSpeed = 0.5f;
[Tooltip("This float represents the number of seconds until the door begins to close. Accounts for the delay in opening at the start.")]
[SerializeField]
private float TimeUntilClose = 7.5f;
[Tooltip("0 = Debug\n1 = Arcane\n2 = Fire\n3 = Ice\n4 = Lightning\n5 = Gamma")]
[SerializeField]
private int ReqDamageType = 0;
[Tooltip("Whether or not the door requires a charged shot to be opened.")]
[SerializeField]
private bool ChargeLock = false;
private float timeOpen = 0;
private bool isOpen = false;
private bool permanent_unlock_flag = false;
// Update is called once per frame
void Update()
{
if (isOpen)
{
// Real-time delay before the door closes, calculated on update ticks
timeOpen += Time.deltaTime;
if (timeOpen >= TimeUntilClose)
{
isOpen = ToggleState();
}
Debug.Log("Closing door. . .");
}
}
public void ApplyDamage (string s)
{
if (isOpen)
return;
Debug.Log("Attempting to open door.");
string[] projectileData = s.Split('/');
// Checks the weapon values against what the door requires to unlock/open
if (!permanent_unlock_flag &&
projectileData[1].Equals("" + ReqDamageType) &&
projectileData[2].Equals("" + ChargeLock))
{
permanent_unlock_flag = true;
ReqDamageType = 0;
ChargeLock = false;
isOpen = ToggleState();
UpdateVisual();
return;
}
else if (permanent_unlock_flag)
isOpen = ToggleState();
}
private bool ToggleState()
{
// moving the door stuff here
return !isOpen;
}
private void UpdateVisual()
{
// change the door graphic to be default non-charge arcane
}
}
}
تعليقات