Gamepad joystick rotation help needed, player turning strangely.
Hi,
I started making my game work with gamepad / controller today, but I'm having problems with getting the player to rotate using the right tumbstick on a gamepad. For keyboard and mouse I had been using ScreenPointToRay to set the players rotation and that worked fine, but I hadn't a clue how to make that work for gamepad so I did some looking around on here and found some code here;
void Rotating (float jH, float jV)
{
if (playerRigidbody.velocity != Vector3.zero) {
Vector3 targetdirection = new Vector3 (jH, 0f, jV);
targetdirection = Camera.main.transform.TransformDirection (targetdirection);
targetdirection.y = 0.0f;
targetdirection.Normalize();
Quaternion targetRotation = Quaternion.LookRotation (targetdirection, Vector3.zero);
Quaternion newRotation = Quaternion.Lerp (playerRigidbody.rotation, targetRotation, 3f * Time.deltaTime);
playerRigidbody.MoveRotation (newRotation);
}
}
Which does work allowing me to rotate the player with the analogue stick, but something isn't right, the player turns but then feels like it's sometimes trying to rotate back against you, or other times will reach a point in the turning cycle and just suddenly speed through the next 90 degrees. And when you let go of the stick the player doesn't stay in the last direction, it randomly keeps rotating for a bit. I'm very confused. I've tried it without using the targetdirectino = Camera.main bit as well, but the same result.
I've tried adjusting the Sensitivity in Input manager, and GetAxis and GetAxisRaw. The setup is an isometric game, so don't know if maybe it's something to do with that.
Really need some help getting this sorted.
What's up with that? Did you try setting up the transform.rotation ins$$anonymous$$d of the Physics? I think Rigidbody.$$anonymous$$oveRotation is supposed to be instantaneous but in my fair bit of experience with manipulating rotation I've never liked using the physics, even for an object otherwise propelled by physics calls.
I've just tried replacing rigidbody.rotation with transform.rotation & transform.rotation = newRotation ins$$anonymous$$d if that's what you mean? But I still get the same result, where I can rotate with the analogue stick, but the player models still doesn't act right, in that it still feels like its fighting back against the direction your turning. And if you release the stick the player model doesn't stay facing that direction, it will slowly rotate to a random direction then stop.
Your answer
Follow this Question
Related Questions
X360 Controller detection gives uneven quadrants 0 Answers
my fps player rotation problem 0 Answers
Rotation is starting at 0, how do I use Math.Clamp() on it? 1 Answer
Rotate Joystick with camera 0 Answers