Prevent Transform Rotation to snap back to 0,0,0 when changing the Control Script
Alright, i have so far programmed a drone simulator, that has a standard set of drone controls. I define a pitch, roll and yaw and then a Quaternion, that takes these three Variables as input, to rotate my Drone. My idea is to simulate an Urban Air Mobility use case rather than the physical behavior of a drone, so kinematic rather than physical movement is enough for my application. Still i have added a RigidBody Component and added a Force in the transform.up local frame, so that i have an acceleration along that Force Vector.
What I wanted to do, and where I'm struggling right now is switching between two different Control Scripts. I have one for Landing and one for Cruising, that have different speed and rotational speed parameters. I have a bool, that checks if I'm in Landing Mode or not. If true, one control script is enabled and the other is not, if false, the other way around.
However, when changing the Control Mode, the rotation of my drone snaps right back to 0,0,0, while the position stays the same. I understand, that this has something to do with the way unity handles rotations, but im still quite lost on how to get around this.
This is my HandleControl() function.
float pitch = input.Cyclic.y * minMaxPitch;
float roll = -input.Cyclic.x * minMaxRoll;
yaw += input.Pedals * yawPower;
finalPitch = Mathf.Lerp(finalPitch, pitch, Time.deltaTime * lerpSpeed);
finalRoll = Mathf.Lerp(finalRoll, roll, Time.deltaTime * lerpSpeed);
finalYaw = Mathf.Lerp(finalYaw, yaw, Time.deltaTime * lerpSpeed);
Quaternion rot = Quaternion.Euler(finalPitch, finalYaw, finalRoll);
rigidbodyComponent.MoveRotation(rot);
I'd be thankful for any input on how i might be able to solve this! Thanks in advance!
Your answer
Follow this Question
Related Questions
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers
Rotate GameObject based on RigidBody Velocity 1 Answer
Rotations on "picked up" object 0 Answers
3D Top Down shooter mouse follow inaccurate at some angles 0 Answers
Rotate Rigidbody towards target rotation using AddTorque() 0 Answers