Character Wont Stop Moving

I’m scripting movement for a 3D model with an animation, and I thought I scripted it at line 33-36 to STOP moving after you let go of the ‘W’ key. It isn’t working. If anyone can help, that would be nice. Thank you.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;
using UnityEngine;

public class KniteController : MonoBehaviour
{
    float speed = 4;
    float rotSpeed = 80;
    float gravity = 8;

    Vector3 moveDir = Vector3.zero;

    CharacterController controller;
    Animator anim;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        anim = GetComponent<Animator>();
    }

    void Update()
    {
        if(controller.isGrounded)
        {
            if(Input.GetKey(KeyCode.W))
            {
                moveDir = new Vector3 (0, 0, 1);
                moveDir *= speed;
            }
            if (Input.GetKeyUp(KeyCode.W))
            {
                moveDir = new Vector3 (0, 0, 0);
            }
        }

        moveDir.y -= gravity * Time.deltaTime;
        controller.Move(moveDir * Time.deltaTime);

    }



}

Maybe you can use GetKeyDown and not GetKey at line 28…

In your script you can set the Move Direction only one time and it will work like a simple GetKey. (I think…)

There is an other way:

Replace the line 33 by an else statement. And the script will do:

GetKey → true → moveDir = new Vector3 …
GetKey → false → moveDir = Vector3.zero;