Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change character Rotation on animation

Discussion in 'Animation' started by marck_ozz, Mar 31, 2020.

  1. marck_ozz

    marck_ozz

    Joined:
    Nov 30, 2018
    Posts:
    107
    Hello everyone, I hope the name of the thread is correct so I can have a little help.

    My issue is that one of my characters has the attack animation (or his avatar) looking backwards from the others animations. It's a free asset from the Store so, I don't wanna complain about this. What I want to do is to change the rotation of this animation, because when the character (enemy) moves towards my player it's ok but when it attacks it do it with his back towards the player.

    I control all the animations with an Animator Controller, so I read about "Apply root Motion" but my enemy's animation doesen't have the option on the animation clip inspector to change it. Neither I can't edit the animation becouse as I said it's a free asset and is not Editable. I also tried by changing the rotation by script with something like this
    Code (CSharp):
    1. Quaternion target;
    2.  
    3.     void Awake()
    4.     {
    5.         target = Quaternion.Euler(0, -1, 0);
    6. _navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    7.         _navMeshAgent.updateRotation = false;
    8. .
    9. .
    10. .
    11. void Attack()
    12.     {
    13.         transform.rotation = transform.rotation * target;
    14. .
    15. .
    16. .
    17. void attackOff()
    18.     {
    19.         transform.rotation = transform.rotation * target;
    20.  
    But any Rotation happend at all.

    I'm ussing Unity 2019.2.2.

    Thank You!!
     

    Attached Files:

    Last edited: Sep 12, 2020
  2. marck_ozz

    marck_ozz

    Joined:
    Nov 30, 2018
    Posts:
    107
    If somebody for some reason fall in this forgoten thread, here is my solution for this:

    just deseable the NavMeshAgent component and rorate the character like this

    Code (CSharp):
    1. void Awake()
    2.     {
    3.         _navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    4.         _navMeshAgent.updateRotation = false;      
    5.     }
    6.  
    7.     //In my case, the attack animation is called in random time
    8. void Update()
    9.     {
    10.             //*****************************Invoke Attak*****************************
    11.             timeAttack -= Time.deltaTime;
    12.             if (timeAttack <= 0 && !isAttacking && !isDeath && !playerScript.death && !playerScript.onDie && !isRotatong && !isDisappeared)
    13.             {
    14.                 isAttacking = true;
    15.                 Invoke("Attack", 0);
    16.                 //Debug.Log("IS ATTACKING");
    17.             }    
    18.     }
    19.  
    20. void Attack()
    21.     {
    22.         _navMeshAgent.enabled = false; // HERE, DISABLE THE NavMeshAgent
    23.      
    24.         rotateBody = true;      
    25.         _animator.SetBool("Attack", true);
    26.         _animator.SetBool("Run", false);      
    27.         _animator.SetBool("Appear", false);
    28.         _animator.SetBool("Disappear", false);
    29.         attacking = true;
    30.         Invoke("attackOff", 2f);  // After a time, the attack animation is over
    31.     }
    32.  
    33. void attackOff()
    34.     {
    35.         _navMeshAgent.enabled = true; // HERE, ENABLE THE NavMeshAgent AGAIN
    36.         //by enabling the meshagent, it take control of the character rotation and movement again
    37.         attacking = false;
    38.         randomTime = Random.Range(3, 6);
    39.         timeAttack = randomTime;
    40.         isAttacking = false;
    41.     }
    42.  
    43.     //I rotate the body in the late update function to be sure the the naveMeshAgent was Enable or disable in the correct order of time
    44. private void LateUpdate()
    45.     {
    46.         if (rotateBody)
    47.         {
    48.             transform.Rotate(0, 180, 0, Space.Self);
    49.             rotateBody = false;
    50.         }
    51.     }
    By doing this, I ensure that the attack animation is always in the Player direction ( it's a 2D like game, because of that the 180° of rotation)
     
    lucasbolbenespro and pasindupra like this.