- Home /
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
}
controller.transform.LookAt(transform.position+direction);
controller.Move((moveDirection*speed)*Time.deltaTime);
}
Any help is greatly appreciated! /Alex
Okay, so I narrowed the problem down to the Joystick Input. It seems that if I hold the left stick up and to the left (northwest, or any other direction) and then let go, one of the axes, Horizontal or Vertical, always drops down to about 0.1 ins$$anonymous$$d of keeping it's value, making the character "snap" roughly 90 degrees. Any ideas on how to fix this?
Answer by whydoidoit · Mar 23, 2013 at 10:51 AM
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 :)
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);
Wow, that last piece of code works like a charm! Thank you. I'm not sure I understand it though. Why do you square the magnitude of the vector? And also, what does the "f" in 0.1f mean? Thank you again. :)
I took the sqr of the magnitude because that's faster than getting the square root and is O$$anonymous$$ for this kind of calculation. The 0.1f is me thinking in C# (that's how you specify a float) sorry about that. Could you tick the answer?
Basically it says don't look at the thing unless you are actually moving the joystick, as in, when the joystick is centered just ignore the direction and leave it like it was the last time.
I need its complete code please help me. I am beginer in coding and unity.
Answer by Toddweiss · Feb 19, 2019 at 09:32 AM
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.
Answer by nextage575 · Nov 07, 2019 at 06:03 AM
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.