Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by johnniks, Oct 23, 2015.

  1. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    FYI I had original posted this in the Help Room but for some reason it vanished... Anyway, 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?

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class FirstPersonController : MonoBehaviour {
    4.    
    5.      public float movementSpeed;
    6.      public float mouseSensitivity;
    7.      public float tilt;
    8.    
    9.      public float upSpeed;
    10.      public float downSpeed;
    11.      private float normal = 0.0f;
    12.    
    13.      CharacterController cc;
    14.    
    15.      void Start() {
    16.          Screen.lockCursor = true;
    17.          cc = GetComponent<CharacterController>();
    18.      }//start end
    19.      void Update() {
    20.        
    21.          //Rotation
    22.          float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
    23.          transform.Rotate(0, rotLeftRight, 0);
    24.        
    25.          float rotUpDown = Input.GetAxis("Mouse Y") * mouseSensitivity;
    26.          transform.Rotate(-rotUpDown, 0, 0);
    27.        
    28.          //Movement
    29.          float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
    30.          float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
    31.        
    32.          if(Input.GetButton("Jump")) {
    33.              normal = upSpeed;
    34.          }
    35.        
    36.                                   //(left/right, up/down, forward)
    37.          Vector3 speed = new Vector3(sideSpeed,0,forwardSpeed);
    38.        
    39.          speed = transform.rotation * speed;
    40.        
    41.          cc.Move(speed * Time.deltaTime);
    42.      }//update end
    43. }//class end
    EDIT: some guy elsewhere suggested this:

    Code (CSharp):
    1. transform.Rotate(0,0,Input.GetAxis("Horizontal") * -45 - transform.eulerAngles.z);
    But that didn't have the desired effect... the tilt is only supposed to make the ship tilt a little from side to side to make it seem more smooth but this that code made 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 S*** and spins around itself...

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

    Code (CSharp):
    1. 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... plus I'm using the CharacterController rather than Rigidbody. But maybe that's a mistake with what I want to achieve?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I'll recommend that you use the transform.rotation as little as possible when it comes to "storing" your rotation between frames. Rather, have a Vector3 representing the direction you're facing towards, then two numbers representing pitch and roll. You can use Vector3.RotateTowards and RotateAround, and Mathf.Lerp to make these movements smooth. And separating these components out like this should give you much more intuitive control over how the rotation behaves.

    I've found that transform.Rotate is very rarely the best way to handle rotation - too much can go wrong and slight errors are cumulative. Better to store data in a way that makes sense for your game, and start the rotation fresh each frame.
    Code (csharp):
    1. Vector3 heading = Vector3.forward;
    2. float pitch = 0f;
    3. float roll = 0f;
    4. void Update() {
    5. //adjust heading, pitch, and roll based on controls here
    6. Vector3 rightVector = Vector3.Cross(heading, Vector3.up); //I guess just take this line on faith, I can't really explain cross vectors, I just use them
    7. transform.rotation = Quaternion.LookRotation(heading) * Quaternion.AngleAxis(pitch, heading) * Quaternion.AngleAxis(roll, rightVector);
    8. }
     
  3. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    Hey StarManta,
    Thanks for sharing but I decided to do something else instead that you may be able to help me with?

    Rather than a tilt I want to make my spaceship roll to the left and to the right (on the z-axis I think, with the usage of Q and E), sorta like this:

    But I don't really know how to go about this :/