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

How do I stop player from moving when he is attacking ?

Discussion in 'Scripting' started by BluesyPompanno, Sep 4, 2017.

  1. BluesyPompanno

    BluesyPompanno

    Joined:
    Jan 1, 2017
    Posts:
    37
    Hello.

    I need help.

    How can i stop calling the function Move when the player is attacking and after he is done attacking call the function back so he can move ?

    I am trying to do this through booleans.But the problem is when the game starts i want the player to be able to move or attack.I don't know how to set this up the right way so the player can attack or move.

    I must be blind.I've tried to set this up through float numbers this doesn't work and it is not a good way to set it up.

    Anybody can help me ?

    This is my code

    Code (CSharp):
    1.  public float walkSpeed = 2;
    2.     public float runSpeed = 6;
    3.     public float gravity = -12;
    4.     public float jumpHeight = 1;
    5.     [Range(0, 1)]
    6.     public float airControlPercent;
    7.  
    8.     public float turnSmoothTime = 0.2f;
    9.     float turnSmoothVelocity;
    10.  
    11.     public float speedSmoothTime = 0.1f;
    12.     float speedSmoothVelocity;
    13.     float currentSpeed;
    14.     float velocityY;
    15.  
    16.    
    17.     public float SwordSwingTime = 2f;
    18.  
    19.     Animator animator;
    20.     Transform cameraT;
    21.     CharacterController controller;
    22.  
    23.     //BOOLEANS//
    24.    public bool CanAttack;
    25.     public bool CanMove;
    26.  
    27.     void Start()
    28.     {
    29.         animator = GetComponentInChildren<Animator>();
    30.         cameraT = Camera.main.transform;
    31.         controller = GetComponent<CharacterController>();
    32.         CanAttack = true;
    33.         CanMove = true;
    34.     }
    35.  
    36.     void Update()
    37.     {
    38.         // input
    39.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    40.         Vector2 inputDir = input.normalized;
    41.         bool running = Input.GetKey(KeyCode.LeftShift);
    42.  
    43.         if(CanMove == true)
    44.         {
    45.             CanAttack = false;
    46.             Move(inputDir, running);
    47.         }
    48.         else { CanAttack = true; }
    49.        
    50.        
    51.        
    52.  
    53.         if (CanAttack == true && Input.GetButtonDown("Fire1"))
    54.         {
    55.             StartCoroutine(SwordAttackQUICK());
    56.             CanMove = false;
    57.  
    58.         }
    59.  
    60.         if (Input.GetKeyDown(KeyCode.Space))
    61.         {
    62.             Jump();
    63.         }
    64.         // animator
    65.         float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / walkSpeed * .5f);
    66.         animator.SetFloat("SpeedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
    67.     }
    68.  
    69.     void Move(Vector2 inputDir, bool running)
    70.     {
    71.         if (inputDir != Vector2.zero)
    72.         {
    73.             float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
    74.             transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
    75.         }
    76.  
    77.         float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
    78.         currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
    79.  
    80.         velocityY += Time.deltaTime * gravity;
    81.         Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
    82.  
    83.         controller.Move(velocity * Time.deltaTime);
    84.         currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;
    85.  
    86.         if (controller.isGrounded)
    87.         {
    88.             velocityY = 0;
    89.         }
    90.  
    91.     }
    92.  
    93.     void Jump()
    94.     {
    95.         if (controller.isGrounded)
    96.         {
    97.             float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
    98.             velocityY = jumpVelocity;
    99.         }
    100.     }
    101.  
    102.     float GetModifiedSmoothTime(float smoothTime)
    103.     {
    104.         if (controller.isGrounded)
    105.         {
    106.             return smoothTime;
    107.         }
    108.  
    109.         if (airControlPercent == 0)
    110.         {
    111.             return float.MaxValue;
    112.         }
    113.         return smoothTime / airControlPercent;
    114.     }
    115.  
    116.  
    117.     IEnumerator SwordAttackQUICK()
    118.     {
    119.         CanMove = false;
    120.         animator.SetBool("Attacking", true);
    121.         animator.SetInteger("RandomAttack", Random.Range(1, 4));
    122.         CanAttack = false;
    123.        
    124.  
    125.         yield return new WaitForSeconds(SwordSwingTime);
    126.  
    127.         animator.SetInteger("RandomAttack", 0);
    128.         animator.SetBool("Attacking", false);
    129.         CanAttack = true;
    130.         CanMove = true;
    131.     }
    132. }
     
  2. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    Try setting controller.enabled to false/true when you don't and do want the player to move
     
    Lethn likes this.
  3. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    You should also check your logic.

    As I see it, CanMove will never be false, because CanAttack will never be true;
    Try this:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         // input
    4.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    5.         Vector2 inputDir = input.normalized;
    6.         bool running = Input.GetKey(KeyCode.LeftShift);
    7.         if (Input.GetButtonDown("Fire1"))
    8.         {
    9.             StartCoroutine(SwordAttackQUICK());
    10.             CanMove = false;
    11.  
    12.         }
    13.         if(CanMove == true)
    14.         {
    15.             Move(inputDir, running);
    16.         }
    17.  
     
    Lethn and BluesyPompanno like this.
  4. BluesyPompanno

    BluesyPompanno

    Joined:
    Jan 1, 2017
    Posts:
    37
    Thank you.It works.I've been trying for hours and couldn't find the right way. :D
     
    sylon likes this.
  5. matheus96brito

    matheus96brito

    Joined:
    Jun 6, 2021
    Posts:
    2