Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rajnine · Aug 10, 2017 at 02:54 AM · animationscripting problemmovementclips

animation only playing for a split second

I found a script online for movement and I liked the way it was set up so I started to mess around with it. My problem is that my "slash" animation only plays for a split second before it switches back to idle. Also some of the transition movements from running back to idle and such are choppy. If anyone knows a fix for either of these it would be greatly appreciated. Thank you

     // Movement Variables:
     private var jumpSpeed:float = 9.0;
     private var gravity:float = 20.0;
     private var runSpeed:float = 5.0;
     private var walkSpeed:float = 1.7;
     private var rotateSpeed:float = 150.0;
     private var grounded:boolean = false;
     private var moveDirection:Vector3 = Vector3.zero;
     private var isWalking:boolean = true;
     private var moveStatus:String = "idle";
 
     private var xSpeed = 250.0;
     private var ySpeed = 120.0;
     private var yMinLimit = -40;
     private var yMaxLimit = 80;
     private var x = 0.0;
     private var y = 0.0;
     var anim : Animator;
      
      
     // -------------------------------------------------------------------------------------------------------------
     // ----------------------------------------------------------- UPDATE ---------------------------------------
     // -------------------------------------------------------------------------------------------------------------
      
     function Update () {
        //
        // Only allow movement and jumps while -----------------  GROUNDED -------------
       anim = GetComponent("Animator");
       if(grounded) {
             moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
            
             // if moving forward and to the side at the same time, compensate for distance
             // TODO: may be better way to do this?
             if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")){
                 moveDirection *= .7;
             }
            
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= isWalking ? walkSpeed : runSpeed;
            
             moveStatus = "idle";
             if(moveDirection != Vector3.zero) {
                 moveStatus = isWalking ? "walking" : "running";
                 if (isWalking){
                 anim.Play("Walk");
                 } else {
                     anim.Play("Run");
                 }
             } else {
             anim.Play("Idle");
                 // call IDLE animation here
             }
             // Jump!
             if(Input.GetButton("Jump"))
             {
             anim.Play("Jump");
                 // call JUMP animation here
                 moveDirection.y = jumpSpeed;
             }if(Input.GetKeyDown(KeyCode.Alpha1))
             {
                 anim.Play("slash");
                 // call JUMP animation here
                
             }
         }                                                                   // END "IS GROUNDED"
        
         // Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
         if(Input.GetMouseButton(1)) {
             transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
         } else {
             transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
         }
        
         // Toggle walking/running with the T key
         if(Input.GetKeyDown("t"))
             isWalking = !isWalking;
            
         //Apply gravity
         moveDirection.y -= gravity * Time.deltaTime;
         //Move controller
         var controller:CharacterController = GetComponent(CharacterController);
         var flags = controller.Move(moveDirection * Time.deltaTime);
         grounded = (flags && CollisionFlags.Below) != 0;
         }
         static function ClampAngle (angle : float, min : float, max : float) {
        if (angle < -360)
           angle += 360;
        if (angle > 360)
           angle -= 360;
        return Mathf.Clamp (angle, min, max);
     }
      
     // -------------------------------------------------------------------------------------------------------------
     // ----------------------------------------------------------- END UPDATE  --------------------------------
     // -------------------------------------------------------------------------------------------------------------
      
     @script RequireComponent(CharacterController)
 
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 N1warhead · Aug 10, 2017 at 07:48 AM

You should really be using proper mecanim workflows. Doing anim.Play does work, but it's not good to do it for multiple potential animations happening, you can't have an idle and slash animation at the same time without problems, because ".play" overwrites the last command and plays the new one. Somewhere Idle is getting called again near instantly, which is because if you are idling when you slash, idle will begin to play again immediately.

I recommend you learn at least how to use bool and float parameters inside the Animator. They will change your world around completely.

Also, not related - but you should really be trying to learn and migrate to C# as Unity is slowly in the process of dropping it. Unless you plan to stay at the engine version you are using then you should really begin C#, I'm only telling you this because I rarely see UnityScript anymore, and don't want to see you in a position where you're like uhhh where's UnityScript without knowing what's coming.

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

215 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple Animation and Scripting Problem. 1 Answer

2 problems with new animation / animator,2 problems with animation 0 Answers

Flicker at the end of rotate 90° with animations 3 Answers

Root motion or script motion to make a character like riven? and any suggestions for learning resources 0 Answers

EditorCurveBinding.FloatCurve propertyName for scale 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