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?
Answer by NoseKills · Sep 17, 2017 at 09:55 PM
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
That's one of the methods I have already attempted as moving the multiplication to any other location results in the same issue. What I gather you are suggesting is:
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.z = Input.GetAxis("Vertical");
Vector3 new$$anonymous$$oveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);
if (new$$anonymous$$oveDirection.magnitude > 1.0f)
{
new$$anonymous$$oveDirection = new$$anonymous$$oveDirection.normalized;
}
new$$anonymous$$oveDirection *= groundSpeed;
Which also results in a dead stop in momentum. Putting it inside the if statement before normalization leads to slow movement. After normalization = doubled speed while running diagonally.
Now that i read this with a bit more thought, the code you posted doesn't show you using new$$anonymous$$oveDirection
s value for anything, you just calculate a value for it, which makes me a bit confused if its value is even causing the problem you are having.
Any non-zero vector can be normalized, making its length exactly 1 no matter if it is diagonal or not so if you use normalized vector (multiplied with something or not) for movement, it will definitely be the same length regardless of direction.
If you multiply first and normalize second, the length of the vector will be 1 regardless of if you multiplied by 2 or 2 million, so that's definitely not the way to go and that's why the character is moving slow.
I haven't used CharacterControllers enough to answer the momentum bit :/
I think I just have to live with the character moving quickly when moving diagonally as there doesn't seem to be a legitimate solution.
Answer by ransomink · Sep 18, 2017 at 02:29 AM
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 );
}
Not sure why my first reply didn't post but anyways. That works for normalizing the movement and allowing for a normal speed. However, when attempting to jump the character controller doesn't go anywhere. It jumps less than an inch and immediately hits the ground. It may be due to noramlizing the y axis? I'm not sure.