Code working on wrong axis.

So, I’m working on a 3d game that involves the player navigating dungeons on a grid based system. I have said movement set up to work using a coroutine. the way it should work is as such: when a button is pressed, a bool “isMoving” is triggered and a function like the following is called:

public void MoveForward()
    {
        StartCoroutine(Move(Vector3.forward, 2.0f));
    }

The values from this are then plugged into this coroutine:

IEnumerator Move(Vector3 axis, float distance)
    {
        

        RaycastHit hit;
        if(Physics.Raycast(transform.position, axis, out hit, distance))
        {
            Debug.Log("Can't move this way.");
            canMove = false;
            yield return null;
        }
        else
        {
            Debug.Log("Can move this way.");
            canMove = true;
            
        }
        if(canMove)
        {
            isMoving = true;
            Vector3 moveDistance = axis * distance;
            Vector3 endLocation = transform.position += moveDistance;

            while (transform.position != endLocation)
            {
                
                transform.position += (endLocation - transform.position) * moveSpeed * Time.deltaTime;
                
            }
            isMoving = false;
            canMove = false;
        }
    }

This code moves the player the proper distance and detects any colliders that are set up to prevent the player from moving. However, I’ve got two problems with this code: 1)when the player is rotated (using another function) the direction of movement doesn’t change; it seems like it’s moving along the world axis instead of it’s local axis. 2) it jumps from the start location to it’s end location instead of smoothly moving.

Problem number 2 isn’t as big of an issue, but I’m not sure how to fix problem 1. Everything seems like it should be working properly, but I am relatively new to coding in C# so I might be mistaken. I thought it might have been that it was expecting me to have it referencing a gameobject, but setting up the code to do so produced the same result, and shouldn’t be needed since the code was directly attached to the object I’m using to represent the player in testing. Any ideas?

Also, I just realized, in my coroutine there’s a value called “moveSpeed”. That’s just a float value which is set to 1.0 by default.

Looks like you are using the World Axis for Forward instead of the local axis of your player.

 StartCoroutine(Move(Vector3.forward, 2.0f));

Change that to use your characters forward. I.E. Character.transform.forward or transform.forward however you are referencing your character.