A little update - Opening Doors! DevLog #5

Published January 17, 2024
Advertisement

This is going to be a little, just to keep me in your minds as I work through some of my backlog, well try to anyway. This one is all about opening doors and using interfaces.

I am using the ‘new’ Unity Input system in my project, I say new, its not at this point - its been available to Devs since I think around 2020 as a verified package. Its taken a little getting used to but I'm confident I'm on the right track with it. After movement, the next most important thing will be to interact with things (OK, so shooting is pretty important in my game too, fine!). There are loads of videos on YouTube as we are most likely well aware and finding one that strikes the right cord is difficult but there are gems out there and one such gem was a video on Interfaces by Rytech - its 2 minutes of your time (I've embedded it at the bottom if you're interested). they're using the older input system but - I know what I'm doing at this point, so my method looks like this;

Player action to invoke interaction.

click the button (mapped to button south on a gamepad and the ‘E’ key on keyboard) → Fire the ray → try and get the interactable interface component (Interfaces are generally defined by an I preceding the name.) from the object. If it does then execute the method Interact() on that object.

Interaction for door.
Interaction for inventory item.

The object has this in its script. In my example I'm checking to see if the door is already open, if its not (isOpen is false) then play the animation, disable the collider and set isOpen to true.

What's great about interfaces is that its easy to implement anything else. As you can see below this is an item that can be stored in an inventory, or if its a quest item fire a different method. but the call from the player input is the same - invoke the interact method.

One thing I have learnt with this method though is that any scripting, colliders and triggers have to be on the parent layer of the object. I've no doubt there is probably a way to manipulate child objects but, I just wanted it working, feel free to drop alternative methods in the comments. I love to know how everyone else goes about this sort of thing.

A bit of a newbie mistake I'm going to admit too; Be careful where you leave your Booleans. I wonder if you can see why this method (below) didn't work

Why didn't this work?

So, by default isOpen is set to false - the door is NOT open. When the player interacts with the door I don't want you spamming the interact key to restart the animation, so lets set a bool value and check - if its true then the door is open and we do nothing. In this case I was setting the value to true BEFORE the condition check - so it was always failing, and the door was not opening!

Anyway, that's all for now, better get back to it, those lights aren't going to bake themselves! until next time.

Here's that interfaces video. I've not been asked to link this, and there are many other videos that cover interfaces in Unity. It helped me, so hopefully it'll help you too. Enjoy.

1 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement