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

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 :confused: 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

EDIT: some guy elsewhere suggested this:

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 shit and spins around itself…

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… plus I’m using the CharacterController rather than Rigidbody. But maybe that’s a mistake with what I want to achieve?

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.

Vector3 heading = Vector3.forward;
float pitch = 0f;
float roll = 0f;
void Update() {
//adjust heading, pitch, and roll based on controls here
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
transform.rotation = Quaternion.LookRotation(heading) * Quaternion.AngleAxis(pitch, heading) * Quaternion.AngleAxis(roll, rightVector);
}

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 :confused: