Ship's rotation

Hi master, I can’t understand why the following code

float pitch = transform.localPosition.y * pitchPositionFactor + yThrow * pitchControlFactor * Time.deltaTime;

why is it necessary to add "transform.localPosition.y * pitchPositionFactor "?
I tried to delete "transform.localPosition.y * pitchPositionFactor ".

float pitch =yThrow* pitchControlFactor * Time.deltaTime;//rotate around X-Axis

and then if the un-pressed the button, the rotation of object would be back to zero. this is that I want.
but when I pressed the button such as W, the value in Y is increased and ship’s head went up. the problem is the rotation is not smooth. it rotated on tiny angle and after a little time rotated again. in this course the button was kept pressing. how can I fix it?

Hi Zhjp,

In many cases, there are multiple ways to make something work in Unity, and Rick cannot show all of them. If your code works, it’s a solution by definition. Also, you don’t have to use Rick’s code if you want to achieve a different behaviour. :slight_smile:

To move our spaceship, the pitch, yaw, roll code is not necessary at all. Rick’s idea was to make the movement more interesting by rotating the ship.

Regarding the problem with your code, Unity internally works with Quaternions, not Euler angles. We need a very low pitch value to achieve what Rick wanted to achieve. Otherwise, his code will not work as intended. Rick’s idea was to make the pitch depend on the localPosition.y, which is the reason why he used localPosition.y in his code.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Compare Rick’s output values to yours. You can simply rename Rick’s variable to pitch2 or something like that. If you don’t use pitch2 anywhere, it will not affect your game but you will be able to log the values into your console.

thank you very much!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms