• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by minimalkid1 · Nov 30, 2014 at 08:42 PM · animationjavascriptsmoothfire1

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image minimalkid1 · Dec 01, 2014 at 12:02 AM 0
Share

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 Main Camera... And if i rotate the camera i cut in back...

avatar image jenci1990 · Dec 01, 2014 at 12:09 AM 0
Share

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);

avatar image minimalkid1 · Dec 01, 2014 at 12:11 AM 0
Share

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 Main Camera... And if i rotate the camera i cut in back... Please help... I try a lot of posibilities but nothing...

avatar image jenci1990 · Dec 01, 2014 at 12:13 AM 0
Share

replace transform.eulerAngles to transform.localEulerAngles

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Activating an Animation? 2 Answers

Scripting "If only" functions? 2 Answers

Why won't it run 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges