Rotation of character resets when joystick is released

Hello! My problem is the following: I’m trying to create a top down controllable character using the CharacterController. My main focus here is making it work with a gamepad (xbox 360 controller). The character rotates and moves perfectly according to left stick input. However, whenever the left stick is released, the character seems to snap right, left, up or down depending on it’s current direction. What I want is to have something similar to Dark Souls or Zelda: Wind Waker, where the character maintains it’s direction when the joystick is released. Here is the Update function of the player object:

function Update () {
	horizontal = Input.GetAxis("Horizontal");
	vertical = Input.GetAxis("Vertical");
	if (controller.isGrounded) { // are we grounded?
		direction = Vector3(horizontal,0,vertical); // set direction
		moveDirection = Vector3(horizontal,moveDirection.y,vertical); // set moveDirection
	} else {
		moveDirection.y -=0.1; // apply gravity

Any help is greatly appreciated!

The joystick must return to 0,0 when it centres - that’s what is has to do otherwise you’d never be able to tell what was going on :slight_smile:

You could:

  • Add the horizontal and vertical values up and store them in a variable
  • Store the largest magnitude value (ignoring sign) and then 0 it if the sign changes.

That last lets you tap one way and tap the other - not sure if that’s what you are after.

However it appears that you want to use it to rotate the character- I’m not 100% sure I quite get my head around how. Perhaps it would be better for you to just not do the LookAt if the magnitude of the vector is small:

  if(direction.sqrMagnitude > 0.1f)
       controller.transform.LookAt(transform.position + direction);

thanks a lot, that really helps me understand thanks a lot guys basically if we are zero on vertical or horizontal then don’t face the start direction and stay in the last know place direction.

i am moving gameObject like this

    float moveHorizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    float moveVertical = CrossPlatformInputManager.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
   transform.rotation = Quaternion.LookRotation(movement);
    transform.Translate(movement * Time.deltaTime * carSpeed, Space.World);

how to fix this same issue it reset when i release joystick.