Gate/lever puzzle script

I got some problems with my lever-gate puzzle.
I am rather new to unity and prefer to work with JS then c#.

Here’s the thing.
I got 4 levers, and 4 gates.

Lever 1 should open/close gate 1 and 4
Lever 2 should open/close gate 1,3 and 4
Lever 3 should open/close gate 2 and 3
Lever 4 should open/close gate 3

Interaction is done with the key “e”.

I have the following animations:
Gate Open
Gate Close
Lever Up
Lever Down

And the following sounds:
GateSound
LeverSound

I got it so far to place a button (cube with no mesh render/box collider) on the lever.
The button works and opens the gate. But they all open together.

private var open : boolean = false;

public var openAnimationString : String;
public var closeAnimationString : String;
public var leverOpenAnimationString : String;
public var leverCloseAnimationString : String;

public var buttonTransform : Transform;
public var distToOpen : float = 6;

private var playerTransform : Transform;
private var cameraTransform : Transform;

public var openSound : AudioClip;
public var closeSound : AudioClip;


function Awake () {
	playerTransform = GameObject.FindWithTag("Player").transform;
	cameraTransform = GameObject.FindWithTag("MainCamera").transform;
	if(open)
		animation.Play(openAnimationString);
		animation.Play(leverOpenAnimationString);
}

function Update () {
	var alreadyChecked : boolean = false;
	if (Vector3.Distance(playerTransform.position,buttonTransform.position) <= distToOpen)
	if (Input.GetKeyDown("e")  !animation.isPlaying)
	{
		if (open)
		{	
			animation.Play(closeAnimationString);
			animation.Play(leverCloseAnimationString);
			open = false;
			alreadyChecked = true;
			if (closeSound)
				audio.PlayOneShot(closeSound);
		}
		if (!open  !alreadyChecked)
		{
			animation.Play(openAnimationString);
			animation.Play(leverOpenAnimationString);
			open = true;
			if (openSound)
				audio.PlayOneShot(openSound);
		}		
	}
}

How are you identifying what gate to open?

I was thinking of making tags for the gates and levers. But I don’t know how that really works in the scripting.

Tags like: Gate1, Gate 2, Lever 1, Lever 2, etc.

Sounds like a case statement should be made;

i.e. case 1 Lever 1 = true;

case Gate 1
false

case Gate 2
true

etc.

If I were writing this script, here’s how I’d do it:

Lever script:
Array of Gate script objects, assigned in editor.
1 function: activate. I would have called it from the player controller (since I might have many different things I could activate with that same button) but your way would also work. When activated, it simply loops through its array of gate scripts and called the toggle position function.

Lever 1’s script would have gates 1 and 4 dragged to it.
Lever 2’s script would have gates 1, 3 and 4 dragged to it.
Etc.

Gate script:
Has 1 public function: Toggle position. It opens the gate if it’s closed, and closes it if it’s open. And plays the appropriate sound for each.

This way, each thing handle its own functions, and the same script works for every instance of the object.

Would’ve done it in an other way.
I would make one separate door handler script which would be attached to all those doors that handles just toggling the door and will remember its own state wether open or close

Then another script which you can assign to all your levers, that will send the command to the door handlers that should work.

So basically doors with i.e. doorscript.cs which contains void ToggleDoor
And the buttons with i.e leverscript which calls ToggleDoor() for the respective doors (by using a few object references)

I think with that construction you can use these scripts multiple times with less hassle :slight_smile:

Haha, I think you must have been writing that at the same time I was. That’s almost exactly what I was saying. :smile:

Hahaha yes indeed, i see it now. Nice :stuck_out_tongue: