• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by johnniks · Oct 21, 2015 at 06:34 PM · c#tiltupdowntilting

How do I tilt my Spaceship and make it go up and down?

Hello, I want my spaceship to tilt left and right whenever I 'sidestep' with it. But I'm not sure how to make that work :/ I would also like to make my spaceship go straight up and down, when pressing 'space' and 'ctrl', but my If-statement doesn't really work >_> any suggestions?

 using UnityEngine;
 using System.Collections;
 
 public class FirstPersonController : MonoBehaviour {
     
     public float movementSpeed;
     public float mouseSensitivity;
     public float tilt;
     
     public float upSpeed;
     public float downSpeed;
     private float normal = 0.0f;
     
     CharacterController cc;
     
     void Start() {
         Screen.lockCursor = true;
         cc = GetComponent<CharacterController>();
     }//start end
     void Update() {
         
         //Rotation
         float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
         transform.Rotate(0, rotLeftRight, 0);
         
         float rotUpDown = Input.GetAxis("Mouse Y") * mouseSensitivity;
         transform.Rotate(-rotUpDown, 0, 0);
         
         //Movement
         float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
         float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
         
         if(Input.GetButton("Jump")) {
             normal = upSpeed;
         }
         
                                  //(left/right, up/down, forward)
         Vector3 speed = new Vector3(sideSpeed,0,forwardSpeed);
         
         speed = transform.rotation * speed;
         
         cc.Move(speed * Time.deltaTime);
     }//update end
 }//class end
Comment
dzone_adm

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by MrMeows · Oct 22, 2015 at 12:53 AM

To tilt the ship...

transform.Rotate(0,0,Input.GetAxis("Horizontal") * -45 - transform.eulerAngles.z);
As for your if statement, it is fine. Just remove " = 0.0f" from line 12 and put "normal = 0;" inside the Start function.
Comment

People who like this

0 Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image johnniks · Oct 23, 2015 at 12:27 PM 0
Share

Hm, the tilt doesn't really do as intended. E.g. if I look straight up and tilt, then the ship goes abe shit and circle around itself. Plus the tilt seems to effect the sideSpeed, which is not okay, it makes the movement very odd... I only wanted the tilt to give the impression of that the player is tilting to the side when turning. I didn't want it to affect the controls... you got any suggestions as how to achieve that? Maybe adding the tilt to the mouse would give the ship a more realistic feel?

And why change "0.0f" to "0"? It's a float after all...

avatar image MrMeows johnniks · Oct 23, 2015 at 04:41 PM 0
Share

Oh, I did not realize the ship could turn up and down. I agree that line would not work in such a case. As for replacing 0.0f with 0, it makes your code look cleaner. Plus, if you really wanted it to be in float form, you could just say 0f.

avatar image johnniks MrMeows · Oct 26, 2015 at 07:13 PM 0
Share

How would you make it go up and down? I've yet to find a solution for this >_>

Show more comments
avatar image johnniks · Oct 23, 2015 at 12:27 PM 0
Share

That didn't have the desired effect... The tilt was only supposed to make the ship tilt a little from side to side to make it seem more smooth but this that code makes the ship follow the tilt, which wasn't the intention... plus if you look straight up and go left or right, then the ship goes ape shit...

I made some code in a 2D setting that had the desired result:

         GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);

But what I want to make now is in a 3D setting...

avatar image

Answer by johnniks · Oct 23, 2015 at 10:56 AM

That didn't have the desired effect... The tilt was only supposed to make the ship tilt a little from side to side to make it seem more smooth but this that code makes the ship follow the tilt, which wasn't the intention... plus if you look straight up and go left or right, then the ship goes ape shit...

I made some code in a 2D setting that had the desired result:

      GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);

But what I want to make now is in a 3D setting...

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by johnniks · Oct 31, 2015 at 08:45 PM

For anyone interested, I made the 'up and down' function work like this:

         if(Input.GetKey("space")) {
             upDownSpeed = movementSpeed * Time.deltaTime;
         } else {
             if(Input.GetKey("left ctrl")) {
                 upDownSpeed = -movementSpeed * Time.deltaTime;
             } else {
                 upDownSpeed = 0.0f;
             }
         }

A special thanks to MrMeows for the assistance :)

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem titling an object from its center (Pitch/Roll) (screenshot) 0 Answers

Continue rotating/ tilting object after key is released? 0 Answers

c# help with rotate and rotatearound 0 Answers

How to tilt 1st person camera's Z axis when moving left and right? 0 Answers

Unity Game object with stop move up and down position in meta world Camera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges