Help, a piece of my movement script isn't working!

Hi, I’m making a 3D platformer script with double jumps and a dash. Currently the dash is not working. Under the “//Dash (Inside IEnumerator)” (or Line 97) Section, an error has appeared on the word “moveDir”. I am unsure on how to resolve this. Appreciate the help!

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

public class PlayerMovement : MonoBehaviour
{
    //Character Controller Variable
    public CharacterController controller;

    //Horizontal Movement Variable
    public float speed;

    //Camera Variables
    public Transform cam;
    public float turnSmoothTime;
    float turnSmoothVelocity;
    Vector3 velocity;

    //Gravity Variable
    public float gravity;

    //Ground Check Variables
    public Transform groundCheck;
    public float groundDistance;
    public LayerMask groundMask;
    bool isGrounded;

    //Double Jump Variables
    public float jumpHeight;
    bool midAirJump;

    //Dash Variables
    public float dashSpeed;
    public float dashTime;


    // Update is called once per frame
    void Update()
    {
        //Horizontal Movement
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
        transform.position += direction * speed * Time.deltaTime;

        //Camera
        if(direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
        }

        //Gravity
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        //Ground Check
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if(isGrounded && velocity.y <0)
        {
            velocity.y = -2f;
        }

        //Double Jump
        if (Input.GetButtonDown("Jump"))
        {
            if (isGrounded)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);   
                midAirJump = true;
            }
            else if (midAirJump)
            {
                velocity.y = Mathf.Sqrt(jumpHeight * -1f * gravity);  
                midAirJump = false;
            }
        }

        //Dash (Inside void update)
        if (Input.GetKey(KeyCode.LeftShift))
        {
            StartCoroutine(Dash());
       
        }
    }

    //Dash (Inside IEnumerator)
    IEnumerator Dash()
    {
        float startTime = Time.time;
        while (Time.time < startTime + dashTime)
        {
            controller.Move(moveDir.normalized * dashSpeed * Time.deltaTime);
            yield return null;
        }


    }
}
if(direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
            controller.Move(moveDir.normalized * speed * Time.deltaTime);
        }

Looks to me like moveDir is only declared and set if direction.magnitude >= 0.1f. What if it is not and that piece of code using moveDir runs? Which i’m sure it is doing, hence the error.

“moveDir” was declared inside of Update, therefore, it only exists inside of Update.

If you want your coroutine to access it, you need to declare the variable at the class-level, where your other “speed”, “gravity”, etc. variables are.

Ahh Okie, I’ll try.

Edit: I ended up doing an alternative, but thanks anyways.