Need more buttons to this script.

So I have this script to open a gate with a button. But the gate should be able to open up with different buttons so I need more then 1.
How am I able to add a size for the amount buttons?

private var open : boolean = false;

//String is used for the animations from the gate and lever to open and close it
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);
		}		
	}
}

1 Answer

1

Hey there,

Not super sure what you’re asking here, but I’ll give it a crack. Do you just want to make it so you can open the gate with an arbitrary number of keys as opposed to just E?

if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.Space) || //Other keys you want to enable) {
     if (!animation.isPlaying) {
          //Gate open function here
     }
}

This will allow you to have different keys in order to open the gate. Also, I would suggest using KeyCode to define keys as opposed to string lookups, as these can be slow. :slight_smile:

Hope that helps,

Klep