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);
}
}
}