hello all, tried figuring this out myself but sadly I cant.
for my project I’m making a leap motion lightsaber game. Got all the leap motion working, I can pick up my lightsaber and press buttons. However I need my button to transform the blade object to scale to look like its going up an down.
Now ive recently managed this with pressing a number key on the keyboard.
using UnityEngine;
using System.Collections;
public class saberScript2 : MonoBehaviour {
private GameObject blade;
private Light saberLight;
public float maxLightIntensity = 3f;
private bool saberOn = false;
private float saberWidth;
// Use this for initialization
void Start () {
blade = this.transform.Find ("blade").gameObject; //finds its child with name "blade" and assign it.
saberLight = GetComponentInChildren<Light> (); //finds its child of light and assign it.
}
// Update is called once per frame
void Update () {
/////////////////////////////////////////////////
/// "vibrating" effect of lightsaber
blade.transform.localScale -= new Vector3(Time.deltaTime * 3f,0,Time.deltaTime * 3f);
if (blade.transform.localScale.x < 1.0f) {
blade.transform.localScale += new Vector3(0.15f,0,0.15f);
}
/////////////////////////////////////////////////
/////////////////////////////////////////////////
/// turn lightsaber off and on. the intensity of the light goes with the length
if (Input.GetKeyDown (KeyCode.Alpha1)) {
if (saberOn)
saberOn = false;
else
saberOn = true;
}
if (saberOn) { //extend the blade, turn on the light
blade.transform.localScale = Vector3.Lerp(blade.transform.localScale, new Vector3(1,1,1), Time.deltaTime * 10f);
if (saberLight.intensity < maxLightIntensity)
saberLight.intensity = blade.transform.localScale.y * maxLightIntensity;
} else { //diminish the blade, turn off the light
blade.transform.localScale = Vector3.Lerp(blade.transform.localScale, new Vector3(1,0,1), Time.deltaTime * 10f);
saberLight.intensity = blade.transform.localScale.y * maxLightIntensity;
}
////////////////////////////////////////////////
}
}
however I’m trying to get this to work for a button, which I cant. I can get the button to do one function which is make the blade go down. as follows.
using UnityEngine;
using System.Collections;
public class saberScript2 : MonoBehaviour {
private GameObject blade;
private Light saberLight;
public float maxLightIntensity = 3f;
private bool saberOn = false;
private float saberWidth;
// Use this for initialization
void Start () {
blade = this.transform.Find ("blade").gameObject; //finds its child with name "blade" and assign it.
saberLight = GetComponentInChildren<Light> (); //finds its child of light and assign it.
}
// Update is called once per frame
public void Button_Click() {
blade.transform.localScale = Vector3.Lerp(blade.transform.localScale, new Vector3(0,0,0), Time.deltaTime * 20f);
saberLight.intensity = blade.transform.localScale.y * maxLightIntensity;
////////////////////////////////////////////////
Debug.Log("LightsaberDeactiavated");
}
}
the question is once that button is pressed and the scale of the object has gone small to the point it looks down, if I was to press the button again the object for re scale making it look like its going up again. Been on this all night and I’m starting to go insane. I have a feeling its something simply but cant manage to figure it out. Any help would be great thanks.