Hello everyone :), I have a problem and I hope you can help me. I have a rigged character that I imported into Unity. I needed an animation for his right arm, a flashlight animation, so I created it inside Unity with Animation. This is how the animation works:
If the flashlight is turned OFF:
1)You press F.
2)He spawns a flashlight, raises his arm up to the level of his shoulder(then the animation is paused), the light turns on, and a click sound-effect plays.
If the flashlight is turned ON:
1)You press F.
2)The light turns off(the click sound-effect plays along), the animation plays in reverse, and when the arm is down again, the flashlight vanishes.
NOW, the animation looks cool, much more it looks REALISTIC even when it plays in reverse, BUT, here comes the problem…:(. Basically the animation will execute only once. I don’t know why, I have no clue.:
THIS means that I press F when the flashlight is turned OFF for the FIRST TIME and the animation plays normally. Then I press F again when the flashlight is turned ON for the FIRST TIME(so basically I press the key “F” for the second time), and the animation plays in reverse just fine. BUT if I press F again(the second time when the flashlight is turned OFF, or I press the key “F” for the third time), the animation won’t play again, and the light won’t turn on. Here’s the script:
using UnityEngine;
using System.Collections;
public class Flashlight : MonoBehaviour
{
public static GameObject PlayerLight;
public GameObject FlashlightObj;
void Start()
{
PlayerLight=GameObject.Find ("/Player/Main Camera/Flashlight");
FlashlightObj.SetActive(false);
}
void Update()
{
if(animation.IsPlaying("FlashlightAnimation"))
{
if(animation["FlashlightAnimation"].speed==1.0f)
{
if(animation["FlashlightAnimation"].time>=0.975f)
{
animation["FlashlightAnimation"].speed=0.0f;
PlayerLight.audio.Play();
PlayerLight.light.intensity=8.0f;
}
}
if(animation["FlashlightAnimation"].speed==-1.0f)
{
if(animation["FlashlightAnimation"].time<=0.05)
FlashlightObj.SetActive(false);
}
}
if(Input.GetKeyDown(KeyCode.F))
{
if(GameStart.ConvStat>3)
{
if(PlayerLight.light.intensity==0.0f)
{
animation.Play("FlashlightAnimation");
FlashlightObj.SetActive(true);
}
else
{
PlayerLight.audio.Play();
PlayerLight.light.intensity=0.0f;
animation["FlashlightAnimation"].time=animation["FlashlightAnimation"].length;
animation["FlashlightAnimation"].speed=-1.0f;
}
}
}
}
}
And no, the variable “ConvStat” has nothing to do with that, because I used “print” to print a message, and even thou the message is printed in the console, the animation still won’t execute the second time. So I don’t understand the problem. Thanks in advance for any help, feel free to ask for details, but I think I told you all you need to know, hopefully.