Help with Tilting Object on its X axis when Pressing Up or Down.

Hi there, I’m Shuroo13

I´m new to programming and I really don’t have much experience better said, I have no experience.

I’m currently learning to code following the course “Create With Code” on the Unity Web Site.

Currently I’m working on my first lab project and I’m trying to make a Side Scroller Space Shooter using the concepts that I have learned.

The Unity version I’m using is 2018.4.16f1 and the project is in 3D, although I have set my camera on a side view to make it look like a 2D game.

I have my player movement going on, and to be honest it’s really basic, most of it comes directly from the lessons themselves, as expected I guess.

I can move my player up and down and left to right, I actually made the adjustments myself to fit my 2D version of the game in a 3D world, so that’s something.

However, I wanted to add a little flare and make my ship tilt when moving up and down, kind of like in the game “Gradius” (if you remember that game) but I’m having a hard time figuring out how to do it.

What I want is to have the Space Ship “tilt” to the left on its x axis when moving up and to the right when its moving down. Same way the SpaceShip in “Gradius” did. Here are some images for reference as to what I want it to do.


This is the View You Would Have on your screen of the spaceship, remember its a 3D world, but the camera view is in 2D. (BTW This is not the actual model, just radom picks to illustrate what I want to do).


This is what I want it to do when it moves “Up” in the screen. Notice how the image shows the bottom part of the shuttle. In real life, this would make the Shuttle move off its axis as its turning to its left, however I just want to simulate like its doing it without actually moving away.


Same thing here, this is what I want it to do when it moves “Down” on the screen, notice how in this image we see the top part of the shuttle making a right turn, i just want to pretend like its doing it.

I came up with the following lines and quickly noticed my ship would go on a rotating frenzy and instead of titling/rotating on its own axis it would start crashing against my background and all over the place. So it was actually not staying in place, but actually doing it and continuously doing it for that matter. So I figured I should probably also try to include some sort of code that prevents this from happening, maybe another if statement, to prevent the ship from rotating endlessly, but to be honest I don’t know how.

In terms of code this is what I tried:


Now, I know this might look stupid to someone experienced, but keep in mind I just started learning, and most of what I know its based either on the course or me trying to decipher the documentation on the website.

I know i’m getting ahead of myself trying to do things I really don´t know about yet, but I also want to keep things fun and if possible turn those quick ideas I have into realities like making a ship tilt, I just want to keep things interesting and know that somehow I’m making progress.

I just want you to know that I tried figuring it out on my own but I cant get past it since i’m still too much of a noob, so that is why I´m asking for guidance. I really like to figure out things on my own since it helps me realize what I’m actually doing wrong vs just copying lines of code, but I really need help on this one.

Any help and or guidance as to how to properly use the documentation on the site, or as to how to make things like this work is much appreciated.

Thanks!

If you need to see the entire Player Controller’s Script that I have so far this is it, still pretty much based on what the tutorial offers, but that is what I have learned so far, in all honesty I had to search about Quaternions I had no idea what those where till now.

Please use code tags.

You are rotating the ship around the x axis. Are you sure that is the right axis to turn it around? Take a Piece of paper, and draw all three axes (x, y, z), and then try to figure out which one to twirl in order to tilt the space ship up or down.

I see no reason in your code above why the ship should go off it’s course and crash into the Background unless you have another movement script attached, your ship moves Independent of its heading. Also, except for rotating around the wrong exis, it should not tumble - it should be set to -30, 0, or 30 degrees as you are setting the rotational value to absolute values. So check if there may be another script attached to some object that controls your ship.

Also, some minor notes: be advised that you set that rotational value every pass through update, while the relevant keys are down. You can use this later to gradually turn the ship into any direction, but you will then have to Change the Notion of how to move formward, and how to store the current Rotation.
Be also advised that you are using two different ways to retrieve horizontal and vertical Input: “Vertical” and “Horizontal” for movement, and keys “W” and “S” for tilting the ship which incidentally may be mapped to “Vertical”.