- Home /
how to add this animation to the knife?
this is the code:
public class KnifeIt : MonoBehaviour
{
float time;
bool onClick = false;
void Update()
{
knifeIt();
if (onClick == true)
{
time += 1;
}
}
void knifeIt()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("hi");
onClick = true;
if (time > 0 && time < 15)
{
transform.Rotate(Vector3.right * -65 * Time.deltaTime);
}
if (time > 15 && time < 30)
{
transform.Rotate(Vector3.right* 65 * Time.deltaTime);
}
if (time == 30)
{
time = 0;
onClick = false;
}
}
}
}
the problem is that the Debug.Log is working, but my knife doesn't move. Anyone help?
I'd look into the mecanim system and animate it that way. This has too many actual numbers in it to be good code. Also, your code logic is frame dependent, this is not going to give you solid results, the knife rotates for exactly 15 frames, then stops. If your frame rate is 100+ you may never actually see it rotating.
Yeah so the short answer is that the getButtonDown is only calling once, only on the frame the mouse button is pressed, so you only start animating for one frame and you'd have to spam the mouse button to make any visible movement. The long answer is that this is a bit of a messy way to do it, see RobAnthem's comment, so consider doing a coroutine or use Unity's built-in animations.
Your answer
Follow this Question
Related Questions
Trying to Make an Animation Work in Unity 5 0 Answers
Character movement distorted when holding weapon 2 Answers
How i can setup Character 0 Answers
Weapons for 3D character 2 Answers
FPS Weapon and animation 1 Answer