Hey all,
I’ve been setting up interactions in our project recently, I have a player class that detects what the player can interact with.
Here are two example scripts (that work without issue)
public class DemoCabinetScript : MonoBehaviour {
private bool cabinetOpen = false;
private GameObject doorOpenerLeft = null;
private GameObject doorOpenerRight = null;
void Awake(){
doorOpenerLeft = GameObject.Find ("DoorOpenerLeft");
doorOpenerRight = GameObject.Find ("DoorOpenerRight");
}
void Update(){
/*
if(Input.GetKeyDown(KeyCode.C)){
if(cabinetOpen){
closeCabinet();
}else{
openCabinet();
}
}
*/
}
public void openCabinet(){
doorOpenerLeft.GetComponent<FPEInteractableActivateScript>().interactionString = "Close cabinet";
doorOpenerRight.GetComponent<FPEInteractableActivateScript>().interactionString = "Close cabinet";
gameObject.GetComponent<Animator>().SetTrigger("OpenCabinet");
}
public void closeCabinet(){
doorOpenerLeft.GetComponent<FPEInteractableActivateScript>().interactionString = "Open cabinet";
doorOpenerRight.GetComponent<FPEInteractableActivateScript>().interactionString = "Open cabinet";
gameObject.GetComponent<Animator>().SetTrigger("CloseCabinet");
}
public void setCabinetOpen(){
cabinetOpen = true;
}
public void setCabinetClosed(){
cabinetOpen = false;
}
public bool isCabinetOpen(){
return cabinetOpen;
}
}
public class DemoCabinetInteractionScript : FPEInteractableActivateScript {
public AudioClip cabinetOpen;
public AudioClip cabinetClose;
private GameObject cabinet;
public override void Awake(){
// Always call back to base class Awake function
base.Awake();
// The cabinet can only be opened with two hands
canInteractWithWhileHoldingObject = false;
cabinet = GameObject.Find("cabinet");
if(!cabinet){
Debug.LogError("DemoCabinetInteractionScript:: Cannot find cabinet Game Object!");
}
}
public override void activate(){
if(cabinet.GetComponent<DemoCabinetScript>().isCabinetOpen()){
cabinet.GetComponent<DemoCabinetScript>().closeCabinet();
gameObject.GetComponent<AudioSource>().clip = cabinetClose;
gameObject.GetComponent<AudioSource>().Play();
}else{
cabinet.GetComponent<DemoCabinetScript>().openCabinet();
gameObject.GetComponent<AudioSource>().clip = cabinetOpen;
gameObject.GetComponent<AudioSource>().Play();
}
}
}
Pretty self-explanatory, the cabinet has two animations, opening and closing, and an idle state.
End result:
aromaticgrippingbuzzard
This works without issue, the problem is, I tried replicating this using a chest and it stopped working for some reason.
Here are the two scripts I’m using for the chest interaction:
public class ChestAnimationInteractable : MonoBehaviour
{
private bool chestOpen = false;
private GameObject ChestOpener = null;
void Awake()
{
ChestOpener = GameObject.Find("ChestOpener");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.C)){
if(chestOpen){
closeChest();
}else{
openChest();
}
}
}
public void openChest()
{
ChestOpener.GetComponent<FPEInteractableActivateScript>().interactionString = "Close chest";
gameObject.GetComponent<Animator>().SetTrigger("ChestOpen");
}
public void closeChest()
{
ChestOpener.GetComponent<FPEInteractableActivateScript>().interactionString = "Open chest";
gameObject.GetComponent<Animator>().SetTrigger("ChestClose");
}
public void setChestOpen()
{
chestOpen = true;
}
public void setChestClosed()
{
chestOpen = false;
}
public bool isChestOpen()
{
return chestOpen;
}
public class ChestInteraction : FPEInteractableActivateScript {
public AudioClip chestOpen;
public AudioClip chestClose;
private GameObject hatch;
public override void Awake()
{
// Always call back to base class Awake function
base.Awake();
// The cabinet can only be opened with two hands
canInteractWithWhileHoldingObject = false;
hatch = GameObject.Find("Hatch");
if (!hatch)
{
Debug.LogError("ChestInteraction:: Cannot find hatch Game Object!");
}
}
public override void activate()
{
if (hatch.GetComponent<ChestAnimationInteractable>().isChestOpen())
{
hatch.GetComponent<ChestAnimationInteractable>().closeChest();
// gameObject.GetComponent<AudioSource>().clip = chestClose;
// gameObject.GetComponent<AudioSource>().Play();
}
else {
hatch.GetComponent<ChestAnimationInteractable>().openChest();
// gameObject.GetComponent<AudioSource>().clip = chestOpen;
// gameObject.GetComponent<AudioSource>().Play();
}
}
}
Everything is exactly the same except for the game objects being manipulated. When I test this out in-game though it doesn’t work as intended.
elasticplayfulamericanriverotter
As you can see, it opens but doesn’t close again. I think the code is functional and I messed up somewhere in the animator.
Here’s the cabinet animator for example:
jampackeddisguisedanemoneshrimp
Here’s the chest animator:
secondfoolishangwantibo
See the difference? One gets stuck and the other doesn’t, even though I double-checked all my settings and everything and all seems the same. So what’s the deal? I also tried resetting the triggers, but I have the same outcome.
What could be wrong here?