Normalized Vector3 Movement Really Slow?

Hi there, so I’m attempting to correct the issue with the provided CharacterController.Move script in the API that causes the controller to move at twice the rate of speed when moving diagonally. I’ve been cracking at it with my Google-Fu for hours to no avail. It seems the solutions that are working for others work for me as well but come with the added pain of being unable to move faster than a crawl. Here’s the current code I’m working with.

 CharacterController controller = GetComponent<CharacterController>();
        if (!controller.isGrounded)
        {
            moveDirection.x = Input.GetAxis("Horizontal") * airSpeed;
            moveDirection.z = Input.GetAxis("Vertical") * airSpeed;
        }
        // If grounded, zero out y component and allow jumping
        else
        {
            moveDirection.x = Input.GetAxis("Horizontal");
            moveDirection.z = Input.GetAxis("Vertical");
            Vector3 newMoveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
            newMoveDirection *= groundSpeed;
            if (newMoveDirection.magnitude > 1.0f)
            {
                newMoveDirection = newMoveDirection.normalized;
            }
            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection.y -= gravity * Time.deltaTime;
        controller.Move(moveDirection * Time.deltaTime);

It may be a bit of a mess due to my persistent fiddling trying to find a solution. At this stage the movement is normalized and you move at the same speed in every direction. It however is extremely slow and adjusting my groundSpeed variable does absolutely nothing. I feel I need an alternative solution to this issue as for whatever reason this hasn’t been working. Any ideas?

With a quick glance, your only mistake is that you multiply before normalizing.

Vector3 newMoveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
newMoveDirection *= groundSpeed;

Let’s say you steer full right, newMoveDirection is (1,0).
If movementSpeed is 100, it becomes (100,0). The magnitude (length of the vector) is now 100 so it gets normalized (maintains direction but length is made 1) so it becomes (1,0) again.

What you (probably) want to do is normalize it if it’s magnitude is over 1 and then multiply with groundspeed.
That makes it so the vector is at most 1 unit in length before the multiplication and, limiting the final length of it to groundSpeed

Dunno why you’d just live with an error or miscalculation. You create Vector3 newMoveDirection and normalize it but you move the player using controller.Move(moveDirection * Time.deltaTime);

Problem 1: You don’t use the Vector3 that you normalized.

Problem 2: You normalize the vector after applying speed (do it at the same time or after).

I’m not sure if the code you have after it will affect the movement because it happens after the normalization, so I placed it at the end. You can see if this works or not.

float h = Input.GetAxis( "Horizontal" );
float v = Input.GetAxis( "Vertical" );

if ( controller.IsGrounded )
{
    if ( Input.GetButton( "Jump" ) )
    {
        moveDirection.Set( h, jumpSpeed, v );
    }
    else
    {
        moveDirection.Set( h, 0, v );
    }

    moveDirection.y -= gravity * Time.deltaTime;
    moveDirection    = transform.TransformDirection( moveDirection );
    moveDirection    = moveDirection.normalized * ( groundSpeed * Time.deltaTime );
    controller.Move( moveDirection );
}