Unwanted up/down (pitch) movement when tilting
Hey All,
since a few days i try to solve a problem but i do not get it. Solved already parts of it but there is one problem remaining. The aim is to move the character per Input.GetAxis Horizontal & Vertical in every direction like in a topdown zelda game. When moving left the character tilts also left, same for right movement. Its already tilting always in the right direction even when flying a circle. Everything works so far the only problem is that i also get an unwanted pitch and the character flys then up and down. But i want to just have a simple movement in all directions and a local tilt on the character without unwanted pitching. I just dont get it how to calculate and fix the unwanted pitch.
Here is my actual code:
public class MovementController : MonoBehaviour
{
private void Start()
{
forward = Camera.main.transform.forward;
forward.y = 0;
forward = Vector3.Normalize(forward);
right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
facingDirection = transform.rotation;
}
private void GetMovementDirection()
{
horizontalDirection = right * Input.GetAxis("Horizontal"); //up - down
verticalDirection = forward * Input.GetAxis("Vertical"); // left - right
movingDirection = Vector3.Normalize(horizontalDirection + verticalDirection);
rotationSpeed = Sensitivity * (Mathf.Clamp01(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")).sqrMagnitude));
}
private void MoveCharacter()
{
//rigidbodyPlayer.MovePosition(rigidbodyPlayer.position + transform.forward * Time.deltaTime * movementSpeed);
rigidbodyPlayer.MovePosition(rigidbodyPlayer.position + movingDirection * Time.fixedDeltaTime);
if (movingDirection != Vector3.zero)
{
facingDirection = Quaternion.LookRotation(movingDirection, Vector3.up);
}
transform.rotation = Quaternion.Slerp(transform.rotation, facingDirection, Time.deltaTime * rotationSpeed);
}
private void TiltCharacter()
{
if (turnLeft)
{
targetRotation = Quaternion.AngleAxis(60f, transform.forward) * transform.localRotation;
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, targetRotation, leftIntensity * 3.0f);
}
else if (turnRight)
{
//same for right movement
}
else
{
// reset position
}
}
}
Maybe someone can push me in the right direction. Thanks in Advance!
Your answer
Follow this Question
Related Questions
How to keep the same yaw(right/left) and pitch(up/down) controls rotation as I roll ? 0 Answers
2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 0 Answers
Tilt object based on movement and direction 1 Answer
Copy Roll From one Gameobject to another Without other rotations effecting it 0 Answers