How to make attack transition?

Hello, I’m fairly new to Unity. I was making an attack transition in my script but in its current state, once I click the left mouse button, my character won’t stop attacking but I’m still able to move and jump and I don’t know how to fix it exacting.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    public float walkSpeed = 10;
    public float runSpeed = 20;
    public float gravity = -20;
    public float jumpHeight = 5;
    [Range(0,1)]
    public float airControlPercent;

    public float turnSmoothTime = 0.2f;
    float turnSmoothVelocity;

    public float speedSmoothTime = 0.1f;
    float speedSmoothVelocity;
    float currentSpeed;
    float velocityY;


    Animator animator;
    Transform cameraT;
    CharacterController controller;

    // Use this for initialization
    void Start ()
    {
        animator = GetComponent<Animator>();
        cameraT = Camera.main.transform;
        controller = GetComponent<CharacterController>();
    }
  
    // Update is called once per frame
    void Update ()
    {
        //Inputs
        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        Vector2 inputDir = input.normalized;
        bool running = Input.GetKey(KeyCode.LeftShift);

        Move(inputDir, running);
        GetInput();

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Jump();
        }
        //Animator
        float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
        animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);

        animator.SetBool("isGrounded", controller.isGrounded);

    }

    //Run
    void Move(Vector2 inputDir, bool running)
    {
        if (inputDir != Vector2.zero)
        {
            float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
            transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
        }

        float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
        currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));

        velocityY += Time.deltaTime * gravity;
        Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;

        controller.Move(velocity * Time.deltaTime);

        if (controller.isGrounded)
        {
            velocityY = 0;
        }

      
    }

    //Jump
    void Jump()
    {
        if (controller.isGrounded)
        {
            float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeight);
            velocityY = jumpVelocity;
        }
    }

    float GetModifiedSmoothTime(float smoothTime)
    {
        if (controller.isGrounded)
        {
            return smoothTime;
        }

        if (airControlPercent == 0)
        {
            return float.MaxValue;
        }

        return smoothTime / airControlPercent;
    }

    //Attack
    void GetInput()
    {
        if (controller.isGrounded)
        {
            if(Input.GetMouseButtonDown(0))
            {
                Attacking();
            }
        }
    }

    void Attacking()
    {
        if(animator.GetInteger("speedPercent") == ) //Don't know what to do here
        animator.SetBool("isAttacking", true);
    }
  }

Hello there!

First of all, welcome to Unity! I hope you enjoy your time here. Anyway, I see what you have done, and it’s just a simple mistake. In your attacking code (although I don’t know what you want to do with that if statement, could you clarify?), you set the isAttacking bool to be true, but you never set it back to being false. If I were you, I’d recommend removing the Attacking void, and trying something like this:

void GetInput(){
if(controller.isGrounded){
if(Input.GetMouseButtonDown(0)){
animator.SetTrigger(“isAttacking”);
}
}
}

A trigger runs once in your animator, and can be used to start an animation, then return back to your original state. I’d recommend this over a boolean as you don’t have to set it back to being false after attacking. However, you can’t hold down the attack button with this - a small downfall. If you’d like me to use a boolean, or need help with anything else, I’d be happy to.

Hopefully this helps with your issue. :slight_smile:

Regards,

Miggi124