Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can lock movement briefly while attacking?

Discussion in 'Scripting' started by DTheChemist, Jan 6, 2023.

  1. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    Id appreciate if someone who has mastered this method of creating an attack hack and slash show me what i need to do to make my attack better.

    I am trying to achieve where when im pressing attack i cant just run and slash. i dont want movement forward or backwards while slashing

    This is what part of what i have now to attack

    Code (CSharp):
    1.  
    2.  
    3. void Update()
    4.     {
    5.  
    6.         if (Input.GetButtonDown("Attack"))
    7.         {
    8.             Attack();
    9.            
    10.         }
    11.  
    12.  
    13.     }
    14.  
    15.  
    16.  
    17.   void Attack()
    18.     {
    19.         if (!isAttacking)
    20.         {
    21.  
    22.             anim.SetTrigger("Attacking");
    23.          
    24.  
    25.         }
    26.      
    27.      
    28.      
    29.     }
    30.  
    31.     void SetAttackTrue()
    32.     {
    33.         isAttacking = true;
    34.      
    35.     }
    36.  
    37.     void SetAttackFalse()
    38.     {
    39.         isAttacking = false;
    40.      
    41.     }
    42.  
    43.  
    44.  
    45.  
    46. }

    whats there is my input to activate attack. Its bool so the animation is happening and the animation event. theres more to the code but im just showing the main parts that makes the character attack. I cant figure out what to do to code it where i can attack and not move. right now im run attacking which i dont want
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    The simplest way is to just set a movement-inhibition timer when you attack, and count that down each frame.

    Code (csharp):
    1. private float inhibitMovement;
    In Update():

    Code (csharp):
    1. if (inhibitMovement > 0)
    2. {
    3.   inhibitMovement -= Time.deltaTime;
    4.   // inhibit movement (eg, ignore it)
    5. }
    6. else
    7. {
    8.   // allow movement here
    9. }
    Otherwise you could use AnimationEvents, which are more setup but more flexible and could be tailored to the animation duration of the attack.

    AnimationEvents:

    https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html

    https://forum.unity.com/threads/no-function-dropdown-in-animationevent.1256793/#post-7987980
     
  3. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    Ok i tried figuring it and got no results. event with the event oddly which i was surprised because my other animation events work without fail

    but script wise i did this and im still able to run glide attack which i dont want

    Code (CSharp):
    1. private float inhibitMovement;

    and in my update i have this

    Code (CSharp):
    1. void Update()
    2. {
    3.  
    4. if (Input.GetButtonDown("Attack"))
    5. {
    6. Attack();
    7.  
    8. }
    9.  
    10. if (inhibitMovement > 0)
    11. {
    12. inhibitMovement -= Time.deltaTime;
    13. canMove = false;
    14. Speed = 0;// inhibit movement (eg, ignore it)
    15. }
    16. else
    17. {
    18. canMove = true;
    19. Speed = 5f;// allow movement here
    20. }
    21.  
    22.  
    23.  
    24. }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Are you increasing the value of
    inhibitMovement
    at any point?
     
    Kurt-Dekker likes this.
  5. DTheChemist

    DTheChemist

    Joined:
    Jan 18, 2021
    Posts:
    115
    yeah at 1 frame i guess i was going for but it didnt work so i left it at zero. im still fresh to this method so i cant honestly say id know exactly what im doing with this script method. for the most part i was using animation events for different things but for some reason i cant get anything i want to happen how i want in this situation
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    All you have to do is when you attack, increase
    inhibitMovement
    by whatever time period you want to prevent attacking for.

    Note that it represents seconds, not frames.