• 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 emilx132 · Sep 19, 2014 at 02:16 PM · animationjavascriptjumpingrunning

Scripting and Animation problems

Hello guys. I need help with some animation and some scripting. I wan't it to run my jump animation when i press space while running or walking. So if im running by pressing W and LeftShift and then wan't to jump then while holding those buttons down i press space to jump. Then i jump but the animation don't play because its running the other animations. It also happens if i walk and press space to jump. It don't play the animation. Only when im standing still or being idle and press space. I would like if u guys could help me with it.

I guess what i need is some kind of a code that checks if im n the air or if im on the ground. Then i need it to say if im in the air it have to pause or stop the walk/run animation and then play the jump animation. Then when im on the ground i wan't it to start the walk/run animation again. of cause if im idle it don't have to play the walk and run animation

This is my script

     #pragma strict
     
     var TheDamage : int = 50;
     var Distance : float;
     var MaxDistance : float = 1.5;
     var TheMace : Transform;
     
     function Update ()
     {
         if (Input.GetButton("Fire1"))
         {
             //Attack animation
             TheMace.animation.Play("Attack");
             //Attack function
             var hit : RaycastHit;
             if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
             {
                 Distance = hit.distance;
                 if (Distance < MaxDistance)
                 {
                     hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
                 }
             }
         } //animation til bevægelse (At gå frem og tilbage med våbnet) og at hoppe.
         
         if (Input.GetButtonUp("Fire1"))
         {
             TheMace.animation.CrossFade("Idle");
         }
         
         if (TheMace.animation.isPlaying == false)
         {
             TheMace.animation.CrossFade("Idle");
         }
         
         if (Input.GetKey (KeyCode.W))
         {
             TheMace.animation.CrossFade("Walk");
             if (Input.GetKey (KeyCode.LeftShift))
             {
                 TheMace.animation.CrossFade("Sprint");
             }
         }
         
         if (Input.GetKey (KeyCode.A))
         {
             TheMace.animation.CrossFade("Walk");
              if (Input.GetKey (KeyCode.LeftShift))
             {
                 TheMace.animation.CrossFade("Sprint");
             }
         }
         
         if (Input.GetKey (KeyCode.S))
         {
             TheMace.animation.CrossFade("Walk");
              if (Input.GetKey (KeyCode.LeftShift))
             {
                 TheMace.animation.CrossFade("Sprint");
             }
         }
         
         if (Input.GetKey (KeyCode.D))
         {
             TheMace.animation.CrossFade("Walk");
              if (Input.GetKey (KeyCode.LeftShift))
             {
                 TheMace.animation.CrossFade("Sprint");
             }
         }
         
         if (Input.GetKeyUp (KeyCode.LeftShift))
         {
             TheMace.animation.CrossFade("Walk");
         }
         
         if (Input.GetKeyUp (KeyCode.W))
         {
             TheMace.animation.CrossFade("Idle");
         }
         
         if (Input.GetKeyUp (KeyCode.S))
         {
             TheMace.animation.CrossFade("Idle");
         }
         
         if (Input.GetKeyUp (KeyCode.A))
         {
             TheMace.animation.CrossFade("Idle");
         }
         
         if (Input.GetKeyUp (KeyCode.D))
         {
             TheMace.animation.CrossFade("Idle");
         }
         
         if (Input.GetKeyUp (KeyCode.Space))
         {
             TheMace.animation.CrossFade("Idle");
         }
         
         if (Input.GetKeyDown (KeyCode.Space))
         {
             TheMace.animation.CrossFade ("Jump");
         }
     }
 
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 betaFlux · Sep 19, 2014 at 03:39 PM

I don't have the solution to your specific problem, this is just a hint: You can omit all GetKeyUp conditions. To make your character idle, just call your movement and shooting conditions and then you simply put your idle animation somewhere, like so:

 void Update()
 {
    if(Input.GetKeyDown(KeyCode.W))
    {
        animation["walk"].speed = 1;
        animation.CrossFade("walk");
    }
    if(Input.GetKeyDown(KeyCode.S))
    {
        animation["walk"].speed = -1;
        animation.CrossFade("walk");
    }
    animation.CrossFade("idle");
 }
Comment
Add comment · 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

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

Attack While Running 1 Answer

Run Animation/Jump animation/Scripting Movement/Animations 0 Answers

Fall animation when character is falling 3 Answers

Textured jump animation moving left and right while in the air -help! 1 Answer

Need help with sprite animating 1 Answer

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