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
2
Question by Juan105ify · Aug 11, 2014 at 07:57 PM · animationmovement

Stopping movement when animation is playing

Hey guys, so I have an issue where my attack animation will play, but the player can still move during the animation. I've tried just setting it's translation to zero, but it still moves. Here my code on how i move and play the animations:

 using UnityEngine;
 using System.Collections;
 
 // Require these components when using this script
 [RequireComponent(typeof(Animator))]
 [RequireComponent(typeof(CapsuleCollider))]
 [RequireComponent(typeof(Rigidbody))]
 public class Wolf_AnimationInput : MonoBehaviour {
 
     public float animSpeed = 1.5f;                // a public setting for overall animator animation speed
     public float lookSmoother = 3f;             // a smoothing setting for camera motion
     public float moveSpeed = 2.0f;
     public float rotateSpeed = 180;
     public float smooth = 0.5f;
 
     private Animator anim;
     private AnimatorStateInfo currentBaseState;// a reference to the animator on the character
     private CapsuleCollider col;        // a reference to the capsule collider of the character
     private CharacterController controller;
     private Transform _mytransform;
 
     static int idleState = Animator.StringToHash("Base Layer.Idle");
     static int runState = Animator.StringToHash("Base Layer.Run");
     static int attackState = Animator.StringToHash("Base Layer.Attacks");
     static float translation;
 
     // Use this for initialization
     void Start () {
 
        
 
         _mytransform = transform;
         anim = GetComponent<Animator>();
         col = GetComponent<CapsuleCollider>();
     
     }
     
     // Update is called once per frame
     void Update () {
 
         translation = Input.GetAxis("Vertical") * moveSpeed;
         currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
 
         MovePlayer();
         PlayerAttack();
         RotatePlayer();
 
         /*         //------Saving for camera use------//
             Quaternion target = Quaternion.Euler(0, tiltAround, 0);
             transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
         */
     }
 
     void MovePlayer()
     {        
         
 
         anim.SetFloat("Speed", translation);
         anim.speed = animSpeed;
 
         translation *= Time.deltaTime;
         transform.Translate(0, 0, translation);
 
         
         
     }
 
     void PlayerAttack()
     {
         if (currentBaseState.nameHash == idleState)
         {
             if (Input.GetButtonDown("Attack") && translation < 0.01)
             {
                 anim.SetBool("Atk", true);
                
             }
             else
             {
                 anim.SetBool("Atk", false);
             }
 
         }
     }
 
     void RotatePlayer()
     {
         if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
         {
             _mytransform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed, 0);
         }
 
         if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
         {
             transform.RotateAround(transform.position, transform.up, 180f);
         }
         else
         {
 
         }
     }
 }
 

 
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
2
Best Answer

Answer by smoggach · Aug 11, 2014 at 08:49 PM

Wrap your MovePlayer() in a check to see if your animation's "Atk" bool is set or not.

 if (!anim.GetBool("Atk"))
      MovePlayer();


or do the same for the relevant section of code inside MovePlayer();

Comment
Add comment · Show 1 · 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 Juan105ify · Aug 13, 2014 at 06:13 PM 0
Share

It works, like the my character stops moving, but now the attack animation I have keeps playing, like the animation is always set back to true.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

SpriteManager 2 1 Answer

Character controller + animation of different meshes making one character 1 Answer

Sprite animation 2 Answers

How to make an enemy pace? 1 Answer

Move Object Using iTween 2 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