- Home /
Animation scripting.
Hello. i work in JavaScripting. I try to animate a knife. I need to change the rotation : x to -50 to cut. But i need to be animated. Smooth move. And before to go rotation x to 0.
cutstep : int = 0;
if(Input.GetButtonDown("Fire1"))
{
if(cutstep == 0)
{
if(SwitchWeapons.Weaponcut == 1)
{
cutstep += 1;
}
}
}
if(cutstep == 1)
{
transform.Rotate(Vector3(-50,0,0));
cutwait();
}
if(cutstep == 2)
{
transform.Rotate(Vector3(50,0,0));
cutwait01();
cutwait02();
}
}
function cutwait()
{
yield WaitForSeconds(0.01);
cutstep = 2;
}
function cutwait01()
{
yield WaitForSeconds(0.01);
}
function cutwait02()
{
yield WaitForSeconds(0.01);
cutstep = 0;
}
It works but make fast move and look ugly... I can't use animation for this object becouse is a prefab ,and don't work i can't make a new animation, and i try to make a script for animation but is so fast.... Thx.
Answer by jenci1990 · Nov 30, 2014 at 11:21 PM
var isAnimate = false;
function Update() {
if (Input.GetButtonDown("Fire1")) {
if (!isAnimate) Cut();
}
}
function Cut() {
isAnimate = true;
yield Animate(new Vector3(0,0,0), new Vector3(-50,0,0), 1);
yield Animate(new Vector3(-50f,0f,0f), new Vector3(0,0,0), 1);
isAnimate = false;
}
function Animate(_from : Vector3, _to : Vector3, _speed : float) {
var timer : float = 0;
while (timer <= 1f) {
timer += 0.01f * _speed;
transform.eulerAngles = Vector3.Lerp(_from, _to, timer);
yield WaitForEndOfFrame();
//or WaitForSeconds(0.01f);
}
transform.eulerAngles = _to;
return;
}
if i press 1 click i cut with knife in front but when i change the position of my character the knife cut in other direction... The knife is attached to $$anonymous$$ain Camera... And if i rotate the camera i cut in back...
In Cut function change the last parameters:
yield Animate(new Vector3(0,0,0), new Vector3(-50,0,0), 1);
yield Animate(new Vector3(-50f,0f,0f), new Vector3(0,0,0), 1);
like this: (2x faster)
yield Animate(new Vector3(0,0,0), new Vector3(-50,0,0), 2);
yield Animate(new Vector3(-50f,0f,0f), new Vector3(0,0,0), 2);
if i press 1 click i cut with knife in front but when i change the position of my character the knife cut in other direction... The knife is attached to $$anonymous$$ain Camera... And if i rotate the camera i cut in back... Please help... I try a lot of posibilities but nothing...
replace transform.eulerAngles to transform.localEulerAngles
Your answer
![](https://koobas.hobune.stream/wayback/20220613172819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Activating an Animation? 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Rotating An Object On Its Axis Once 3 Answers