- Home /
Why is my character controller moving in unrelated directions?
I tested my movement script with the following code (condensed for length), and without ever pressing "W" or "S," the player somehow manages to move forwards. I have also attached two images. In both images, the character controller's path is portrayed by a line renderer which follows the player. Additionally, in both cases the player only presses "A" and "D." In the second image, the player is rotated by exactly 54.609 degrees on the y axis. The difference being that when facing completely forwards, the issue where the player moves forward does not occur; why is the player moving forwards when pressing a/d as depicted in image 2?
I am attempting to avoid using transformDirection(velocity) on every characterController.Move() call, so that the player continues travelling in the same direction regardless of the rotation of the player, which is more or less the law of inertia.
if (Input.GetKey(KeyCode.W))
{
velocity += xzMover.transform.forward * accelerationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
velocity -= xzMover.transform.forward * accelerationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.D))
{
velocity += xzMover.transform.right * accelerationSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
velocity -= xzMover.transform.right * accelerationSpeed * Time.deltaTime;
}
playerController.move(velocity * Time.deltaTime);
Edit: Tested it with TransformDirection(x, 0, z), and got exactly the same results. For each keypresses, the velocity increased by transformDirection(accelerationSpeed, 0, accelerationSpeed), replacing either the x or the z with 0 depending on whether the player was increasing velocity on the x or z axis.
Answer by Bunny83 · Dec 04, 2019 at 06:19 AM
I would simply assume that you move on a slope? So you naturally move down the slope due to the collision with the ground.
Well, if that's not the case we have to know more about where else you modify your velocity. At the moment you only accelerate into the specified direction. If you don't have any kind of deceleration (drag, friction) once you have some velocity you won't get rid of it. If you actually implemented some kind of drag it depends on how you implemented it. Note that even it looks like you have stopped moving it doesn't mean you really stopped moving.
If the ground is not tilted, are you sure your character isn't as well? So it has 0 rotation on x and z but only on y? $$anonymous$$eep in $$anonymous$$d that the character controller can only rotate around y. Have you checked what values transform.forward and transform.right return? The y component should be 0,
I am not even rotating the character controller, actually. I use a child gameobject called "xz$$anonymous$$over" which only rotates on the y axis, in order to deter$$anonymous$$e the direction.
The transform of the general object never changes except position-wise; rotation stays the same.
I have actually disabled friction entirely for the moment, the only velocity changes are the player being constantly pushed into the ground by gravity, and what keys the player presses.
Your answer
Follow this Question
Related Questions
Can't fix slow jumping for character controller 3D 1 Answer
Objects stuck dragging across floor 1 Answer
How to let a GameObject generate force but not be affected by certain forces? 0 Answers
Crouching makes my player unable to move. 2 Answers
How to transform velocity to world space before calling controller.move? 1 Answer