Hello,
I have a problem with the movements of my object (in this particular case the Player character).
Short Version: Moving works fine so far, but when running diagonally, the character moves faster.
Long Version: I've scripted a Controller and Camera View for an isometric like game, like typical in older console RPGs. The Camera points in a fixed direction and moves with the character and can't be rotated (for now). The Character basically moves along the camera axis, so pressing W will always move the character in the direction of top of the screen, D will always move it to the right etc.
This works perfectly so fine. Well, almost. Problem is, when the character is moving diagonally, he moves faster. I already go an idea why it's so, however I haven't found a good solution yet to scale it down.
But take a look at the code below:
// Update is called once per frame
void Update () {
// update movement only when the character is grounded
if(charController.isGrounded == true) {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// Find out the direction in which the camera is pointing
Vector3 forward = (cameraTransform.forward*2-cameraTransform.forward);
Vector3 right = (cameraTransform.right*2-cameraTransform.right);
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
moveDirection = (forward * vertical)+(right * horizontal);
// change direction of the player to the walking direction
transform.forward = moveDirection;
// save last position for colider
lastPosition = charController.transform.position;
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * (Time.deltaTime * movementSpeed));
}
As you can see, I first camera forward and right vectors and apply the values of the input axis. Well the problem is, i guess, that when the character moves i.e. 4 units to the right and 4 units to the top, he moves ~5.65 units diagonally (Pythagoras, c^2 = a^2+b^2).
So my first thought was, to find out the point where that diagonal line hits a 1/4 circle line and then calculate the new x and y values out of it. But I'm quite unsure where to start and how to exactly calculate that point (or alternatively scale down the above Vector).
Any ideas?