- Home /
Movement/Acceleration problems
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?
Answer by Statement · Dec 06, 2010 at 08:35 PM
Short answer for short version:
moveDirection = (forward * vertical)+(right * horizontal);
moveDirection.Normalize();
Thanks! Now that you post it, I actually realized my error. Was pretty stupid kind of. I had Vector3.Normalize(moveDirection)
somewhere there, but no assignment (as it originally was placed somewhere else and I later refactored the code but didn't change that to moveDirection.Normalize()
or moveDirection = Vector3.Normalize(moveDirection)
xD
It's easy to get code blind :) Glad you can move on now :)
Your answer
Follow this Question
Related Questions
Wheel rotation help cant work out the problem? 0 Answers
Rigidbody regenerated acceleration movement ? 0 Answers
Accelerate slowdown Player movement 0 Answers
Acceleration with CC 0 Answers
Character Movement 2 Answers