Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 convictcartel · Jan 31, 2014 at 10:37 PM · animationbuttonattack

Play Animation on Button Press

I have a bit of a problem, im making a mobile touch pad, so when you use the touch pad the walk animation plays.. when your not touching it the idle animation plays, then I added a onGUI button to play attack when pressed but its not working because the other animations come in the way, I cant figure out how to fix it.. any help??

 #pragma strict
 
 @script RequireComponent( CharacterController )
 
 // This script must be attached to a GameObject that has a CharacterController
 var moveTouchPad : EasyMobileJoystick;
 var jumpTouchPad : EasyMobileJoystick;
 
 var forwardSpeed : float = 4;
 var backwardSpeed : float = 4;
 var jumpSpeed : float = 16;
 var inAirMultiplier : float = 0.25;                    // Limiter for ground speed while jumping
 
 var btnTexture : Texture;
 var attack = true;
 
 private var thisTransform : Transform;
 private var character : CharacterController;
 private var velocity : Vector3;                        // Used for continuing momentum while in air
 private var canJump = true;
 
 function Start()
 {
     // Cache component lookup at startup instead of doing this every frame        
     thisTransform = GetComponent( Transform );
     character = GetComponent( CharacterController );    
 
     // Move the character to the correct start position in the level, if one exists
     var spawn = GameObject.Find( "PlayerSpawn" );
     if ( spawn )
         thisTransform.position = spawn.transform.position;
 }
 
 function OnEndGame()
 {
     // Disable joystick when the game ends    
     moveTouchPad.Disable();    
     jumpTouchPad.Disable();    
 
     // Don't allow any more control changes when the game ends
     this.enabled = false;
 }
 
 function Update()
 {
     var movement = Vector3.zero;
 
     // Apply movement from move joystick\
     
     //ATTACK BUTTONS
 
             //
     if ( moveTouchPad.position.x > 0 && attack == true)
      transform.rotation = Quaternion.Euler(0,90,0);
     animation.Play("walk");
     
     if ( moveTouchPad.position.x < 0 && attack == true)
     transform.rotation = Quaternion.Euler(0,-90,0);
     animation.Play("walk");
     
     if ( moveTouchPad.position.x == 0 && attack == true)
     animation.Play("idle");
     
     if ( moveTouchPad.position.x > 0 )
         movement = Vector3.right * forwardSpeed * moveTouchPad.position.x;
     else
         movement = Vector3.right * backwardSpeed * moveTouchPad.position.x;
     
     // Check for jump
     if ( character.isGrounded )
     {        
         
         var jump = false;
         var touchPad = jumpTouchPad;
             
         if ( !touchPad.IsFingerDown() )
             canJump = true;
         
          if ( canJump && touchPad.IsFingerDown() )
          {
              print("Jump pressed");
             jump = true;
             canJump = false;
          }    
         
         if ( jump )
         {
             print("Jump Fired");
             // Apply the current movement to launch velocity        
             velocity = character.velocity;
             velocity.y = jumpSpeed;    
         }
     }
     else
     {            
         // Apply gravity to our velocity to diminish it over time
         velocity.y += Physics.gravity.y * Time.deltaTime;
                 
         // Adjust additional movement while in-air
         movement.x *= inAirMultiplier;
 //        movement.z *= inAirMultiplier;
     }
         
     movement += velocity;    
     movement += Physics.gravity;
     movement *= Time.deltaTime;
     
     // Actually move the character    
     character.Move( movement );
     
     if ( character.isGrounded )
         // Remove any persistent velocity after landing    
         velocity = Vector3.zero;
 }
 
     function OnGUI() {
         if (GUI.Button(Rect(10,10,50,50),btnTexture))
         attack = false;
                 animation.Play("attack");
             }
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
1
Best Answer

Answer by supernat · Jan 31, 2014 at 10:53 PM

You can play multiple animations and just blend to the attack animation, then blend it back out once the attack is complete. See Animation.Blend() for more information: http://docs.unity3d.com/Documentation/ScriptReference/Animation.Blend.html

See this page on how to use Play() properly with a layer and blending: http://docs.unity3d.com/Documentation/ScriptReference/Animation.Play.html

Additionally, you may want to use the built-in methods like CrossFade: http://docs.unity3d.com/Documentation/ScriptReference/Animation.CrossFade.html

In general, take an in-depth look at the Animation reference: http://docs.unity3d.com/Documentation/ScriptReference/Animation.html

The most likely problem you see right now is that your animation is set to play mode StopSameLayer.

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.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button press for Animations 1 Answer

What am I doing wrong with this attack script? 2 Answers

Touch button to transition through Animations 1 Answer

Spritesheet animation shake/jitter/wobble problem on android. 1 Answer

ui button animation finish 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges