Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug My character teleporting when i play attack animation

Discussion in 'Animation' started by jackwak, Nov 17, 2023.

  1. jackwak

    jackwak

    Joined:
    May 25, 2023
    Posts:
    1
    When transitioning from the idle animation to the attack1 animation, my character sometimes teleports a bit to the right. While running, it teleports a bit more forward. I have tried disabling all components on the character, but even when playing only the animation, I still encounter the same issue.


    character have PlayerCombo2 and Character_Controller scripts
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerCombo2 : MonoBehaviour
    6. {
    7.     private enum State
    8.     {
    9.         ATTACK,
    10.         MOVE
    11.     }
    12.  
    13.     [SerializeField]int comboCounter = 1;
    14.  
    15.     private State state;
    16.     private Character_Controller character_Controller;
    17.     private Animator animator;
    18.  
    19.     private void Start()
    20.     {
    21.         animator = GetComponent<Animator>();
    22.         character_Controller = GetComponent<Character_Controller>();
    23.  
    24.         state = State.MOVE;
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         if (Input.GetButtonDown("Fire1"))
    30.         {
    31.             state = State.ATTACK;
    32.             ChooseAttackId();
    33.             Attack(comboCounter);
    34.  
    35.         }
    36.         if (animator.GetAnimatorTransitionInfo(0).IsName("Attack1 -> Exit") || animator.GetAnimatorTransitionInfo(0).IsName("Attack2 -> Exit") || animator.GetAnimatorTransitionInfo(0).IsName("Attack3 -> Exit"))
    37.         {
    38.             ResetCombo();
    39.             state = State.MOVE;
    40.  
    41.         }
    42.  
    43.         switch (state)
    44.         {
    45.             case State.ATTACK:
    46.                 character_Controller.input = Vector3.zero;
    47.                 character_Controller.canMove = false;
    48.                 animator.applyRootMotion = true;
    49.  
    50.  
    51.                 break;
    52.             case State.MOVE:
    53.                 animator.applyRootMotion = false;
    54.                 character_Controller.canMove = true;
    55.                 animator.SetBool("isRunning", character_Controller.input.magnitude > 0.5f);
    56.  
    57.                 break;
    58.             default:
    59.                 break;
    60.         }
    61.     }
    62.  
    63.  
    64.     void Attack(int attackId)
    65.     {
    66.         animator.SetBool("Attack" + attackId, true);
    67.         StartCoroutine(FalseAttackState("Attack" + attackId));
    68.  
    69.         if ("Attack" + attackId == "Attack3")
    70.         {
    71.             ResetCombo();
    72.         }
    73.     }
    74.  
    75.     int ChooseAttackId()
    76.     {
    77.         if (animator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
    78.         {
    79.             if (animator.GetCurrentAnimatorStateInfo(0).IsName("Attack1"))
    80.             {
    81.                 comboCounter = 2;
    82.             }
    83.             else if (animator.GetCurrentAnimatorStateInfo(0).IsName("Attack2") || animator.GetAnimatorTransitionInfo(0).IsName("Attack2"))
    84.             {
    85.                 comboCounter = 3;
    86.             }
    87.         }
    88.         return comboCounter;
    89.     }
    90.  
    91.     void ResetCombo()
    92.     {
    93.         comboCounter = 1;
    94.     }
    95.  
    96.     IEnumerator FalseAttackState(string attackState)
    97.     {
    98.         yield return new WaitForSeconds(0.5f);
    99.         animator.SetBool(attackState, false);
    100.     }
    101. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Character_Controller : MonoBehaviour
    6. {
    7.     private Rigidbody rb;
    8.  
    9.     [SerializeField] private float speed = 5;
    10.     [SerializeField] private float turnSpeed = 2000;
    11.     public Vector3 input;
    12.     public bool canMove;
    13.  
    14.  
    15.  
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.  
    20.         canMove = true;
    21.     }
    22.  
    23.     private void FixedUpdate()
    24.     {
    25.         Move();
    26.        
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         GatherInput();
    32.         Look();
    33.     }
    34.  
    35.     //savaşırken apply root motion ı aç
    36.  
    37.     void GatherInput()
    38.     {
    39.         input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    40.     }
    41.  
    42.     void Move()
    43.     {
    44.         if (canMove)
    45.         {
    46.             rb.MovePosition(transform.position + (transform.forward * input.magnitude).normalized * speed * Time.deltaTime);
    47.         }
    48.     }
    49.  
    50.     void Look()
    51.     {
    52.  
    53.         if (input != Vector3.zero)
    54.         {
    55.             var relative = input.ToIso();
    56.             var rot = Quaternion.LookRotation(relative, Vector3.up);
    57.  
    58.             transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, turnSpeed * Time.deltaTime);
    59.         }
    60.  
    61.     }
    62.  
    63.    
    64.  
    65.  
    66. }