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 yigitkahramn · Aug 08, 2018 at 12:29 PM · animatorcontroller

canMove bool never turn true after attack animation.

I have a simple problem which is while player is attacking i want move function to stop(that i accomplish); after that move function doesn't work. here is my code. if anyone can point me to right direction, it'd be so good for me. thank you.

 public class Controller : MonoBehaviour {
 
     public float moveSpeed, turnSpeed;
     public float height = 0.5f, heightPadding = 0.05f, maxGroundAngle = 120;
     float angle, moveAmount, actionDelay;
     float groundAngle;
     public LayerMask ground;
     public bool debug;
     bool grounded;
 
     [HideInInspector]
     public bool input_LM, input_RM, input_Space, input_LShift, input_E, input_Q, input_F;
     public bool inCombat, equipped, lockedOn, canMove, inAction;
 
     Vector3 forward;
     RaycastHit hitInfo;
     Vector2 input;
     Quaternion targetRotation;
     Transform cam;
     Rigidbody rigid;
     Animator anim;
 
     void Start(){
         canMove = true;
         cam = Camera.main.transform;
         rigid = GetComponent<Rigidbody>();
         rigid.angularDrag = 999;
         rigid.drag = 4;
         anim = GetComponentInChildren<Animator>();
     }
 
     void Update(){
         GetInput();
         HandleAnimations();
         if (inAction)
             canMove = false;
         else
             canMove = true;
         CalculateDirection();
         CalculateForward();
         CalculateGroundAngle();
         CheckGround();
         if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) 
             return;
         Rotate();
         if(canMove == true)
             Move();
         if (Input.GetButton("Interact"))
             Interact();
     }
 
     void LateUpdate(){
         
     }
 
     void GetInput(){
         input.x = Input.GetAxisRaw("Horizontal");
         input.y = Input.GetAxisRaw("Vertical");
         input_LM = Input.GetButtonUp("Left Mouse");
         input_RM = Input.GetButtonUp("Right Mouse");
         input_LShift = Input.GetButtonUp("Dash");
         input_Space = Input.GetButtonUp("Roll");
         input_Q = Input.GetButtonUp("Cast Left");
         input_E = Input.GetButtonUp("Cast Right");
         input_F = Input.GetButtonUp("Interact");
     }
 
     void CalculateDirection(){
         angle = Mathf.Atan2(input.x, input.y);
         angle = Mathf.Rad2Deg * angle;
         angle += cam.eulerAngles.y;
     }
 
     void Rotate(){
         targetRotation = Quaternion.Euler(0, angle, 0);
         rigid.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
     }
 
     void Move(){
         if (groundAngle >= maxGroundAngle)
             return;
         rigid.MovePosition(transform.position + forward * moveSpeed * Time.deltaTime);
     }
 
     void CalculateForward(){
         if (!grounded){
             forward = transform.forward;
             return;
         }
         forward = Vector3.Cross(transform.right, hitInfo.normal);
     }
 
     void CalculateGroundAngle(){
         if (!grounded){
             groundAngle = 90;
             return;
         }
         groundAngle = Vector3.Angle(hitInfo.normal, transform.forward);
     }
 
     void CheckGround(){
         if (Physics.Raycast(transform.position, -Vector3.up, out hitInfo, height + heightPadding, ground)){
             grounded = true;
         }else{
             grounded = false;
         }
     }
 
     void DrawDebugLines(){
         if (!debug)
             return;
         Debug.DrawLine(transform.position, transform.position + forward * height, Color.blue);
         Debug.DrawLine(transform.position, transform.position - Vector3.up * height, Color.green);
     }
 
     void Interact(){
         
     }
 
 
     public void CheckForEquip(){
         if (!inCombat){
             equipped = false;
             lockedOn = false;
         }else{
             equipped = true;
         }
     }
 
     void HandleAnimations(){
         CheckForEquip();
         //movement
         anim.SetFloat("Horizontal", Mathf.Abs(input.x));
         anim.SetFloat("Vertical", Mathf.Abs(input.y));
         //actions
         if (input_LM == false && input_RM == false && input_E == false
             && input_Q == false && input_F == false && input_Space == false && input_LShift == false){
             return;
         }
         string targetAnimation = null;
         if (input_LM){
             inAction = true;
             targetAnimation = "Attack_05";
         }
         if (input_RM){
             inAction = true;
             targetAnimation = "Attack_01";
         }
         anim.CrossFade(targetAnimation, 0.2f);
         rigid.velocity = Vector3.zero;
         if (inAction){
             anim.applyRootMotion = true;
             Vector3 delta = anim.deltaPosition;
             delta.y = 0;
             Vector3 v = (delta * 1) / Time.deltaTime;
             rigid.velocity = v;
             rigid.drag = 0;
         }
         anim.applyRootMotion = false;
     }
 }
Comment
Add comment · Show 2
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 Chronos-L · Aug 08, 2018 at 12:59 PM 0
Share

You didn't set inAction = false in any part of your code.

avatar image yigitkahramn Chronos-L · Aug 08, 2018 at 01:03 PM 0
Share

where should i do that?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by niiicolai · Aug 08, 2018 at 01:45 PM

You will have to set inAction back to false after your attack animations is finished. For this you could fetch the animation lengths and instead run your attack behaviour in a coroutine.

So for the example below you have to enter the correct clip names of the animations you use for animator states Attack_01 and Attack_05

 Animator animator;
 bool inAction;
 bool canMove;
 float fadeLength = .2f;
 List<string> animationStateNames = new List<string>() {"Attack_01", "Attack_05"};
 List<string> animationClipNames = new List<string>() {"Animation_clip_name_1", "Animation_clip_name_2"};
  List<float> animationLengths = new List<float>();

 void Start () {
     animator = GetComponent<Animator>();

     foreach(AnimationClip animationClip in animator.runtimeAnimatorController.animationClips) {            
         for (int i = 0; i < animationClipNames.Count; i++) {
             if (animationClip.name == animationClipNames[i]) {
                 animationLengths.Add(animationClip.length);
             }
         }
     }
 }

 void Update() {
     
     // Only run the attack behaviour if we isn't attacking allready.
     var leftClick = Input.GetMouseButtonDown(0);
     var rightClick = Input.GetMouseButtonDown(1);
     if (!inAction && leftClick) {
         StartCoroutine(Attack(1));
     } else if (!inAction && rightClick) {
         StartCoroutine(Attack(0));
     }

     if (inAction) {
         // run some behaviour every frame if we are in action.
     }
 }

 // https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
 IEnumerator Attack(int animationIndex) {    
     inAction = true;
     canMove = false;
     animator.CrossFade(animationStateNames[animationIndex], fadeLength);
     
     yield return new WaitForSeconds(animationLengths[animationIndex]);
     
     inAction = false;
     canMove = true;
 }
Comment
Add comment · Show 14 · 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 yigitkahramn · Aug 08, 2018 at 02:05 PM 0
Share

Thank you for your time!

But now there is a "Argument out of range exception" error for "StartCoroutine(Attack(animations[1], animationLengths[1]));" and "StartCoroutine(Attack(animations[0], animationLengths[0]));".

avatar image niiicolai yigitkahramn · Aug 08, 2018 at 02:07 PM 0
Share

Hmm. It doesn't find any animation lengths then. 2 sec.

avatar image niiicolai yigitkahramn · Aug 08, 2018 at 02:44 PM 1
Share

@yigitkahramn Check my updated anwser

avatar image yigitkahramn niiicolai · Aug 08, 2018 at 03:54 PM 0
Share

it is same error again and also can$$anonymous$$ove is not returning true anymore. i don't get it; it seems fine to me. :/

Show more comments

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

108 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

Related Questions

How to add an animator controller to animator by script in unity3d? 1 Answer

Keeping 4-Legged entity grounded 0 Answers

Does anyone have an example of an animator with diagnol locomotion? 0 Answers

Avatar Masks and Animator Override Controllers 0 Answers

Dynamically add AnimationState to Controller 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