hold button to stop spin then re-enable, script help
I have used this script I found on here http://answers.unity3d.com/questions/ask.html?space=8
I am making a slot machine.
i'm trying to make hold buttons to press and stop one of the 3 reels from spinning while the hold button is active.
this is my reel spinning scipt:
#pragma strict
var reel_1 : GameObject;
var mySound: AudioClip;
var ac : AnimationCurve;
private var spinning = false;
private var ray : Ray;
private var rayCastHit : RaycastHit;
function DoSpin(time : float, maxAngle : float) {
spinning = true;
var timer = 0.0;
var startAngle = transform.eulerAngles.y;
while (timer < time) {
var angle = ac.Evaluate(timer/time) * maxAngle;
reel_1.transform.eulerAngles = Vector3(angle + startAngle ,0.0, 90.0);
timer += Time.deltaTime;
yield;
}
reel_1.transform.eulerAngles = Vector3(maxAngle + startAngle ,0.0, 90.0);
spinning = false;
}
function Update() {
if(Input.GetMouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit)){
//add name of object/button to affect in ""
if(rayCastHit.transform.name == "spin_button" && !spinning){
DoSpin(3, 2160.0);
//play sound
//audio.clip = mySound;
//audio.Play();
}
}
}
}
and this is my script for my hold button:
private var ray : Ray;
private var rayCastHit : RaycastHit;
var mySound: AudioClip;
var hold_lit : GameObject;
var reel_1 : GameObject;
var held : boolean;
function Update(){
if(Input.GetMouseButtonDown(0)){
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit)){
//add name of object/button to affect in ""
if(rayCastHit.transform.name == "hold_button_1"){
held = !held;
if(held){
hold_lit.active = true;
var varGameObject = gameObject.Find("reel_1");
varGameObject.GetComponent(spin_script_reel_1).enabled = false;
audio.PlayOneShot(mySound);
reel_1.collider.enabled = false;
}
if(!held){
hold_lit.active = false;
var varGameObject1 = gameObject.Find("reel_1");
varGameObject1.GetComponent(spin_script_reel_1).enabled = true;
audio.PlayOneShot(mySound);
reel_1.collider.enabled = true;
}
}
}
}
}
When I press the hold button, It turns off my spin script (unticks it in the inspector) and when I spin the reels it stays as it should.
However, when I press hold button again it turns on the spin script (ticked in inspector) as it should, but when I press spin to spin the reels the previously held reel stays in place as if the script is not running.
Can anyone see the cause of this? please help it's driving me crazy lol :)
Your answer
Follow this Question
Related Questions
Manual activation error - Hwid ulf can't be blank 0 Answers
Problems with debug and if-statement 1 Answer
How do I make a gun reload after 30 shots? 4 Answers
If statement not working 1 Answer
Get Time A Bool Has Been True 4 Answers