Spacecraft script WIP seeking advice

Okay, after allot of work (from a beginner mind you) I have a crude, semi-working spaceship control script, with the help of the forums as always, and I referenced Tiles airplane script quiet a bit, (Good stuff man). I still have a couple major issues, the most obvious at the moment is that when flying down and turning left or right the ship doesn’t really turn left or right, I’m not exactly sure whats causing it, you can be sure I’ll be digging around in there trying to figure it out, but if anyone wants to drop me a tip or two I’d love it, Here’s the script, feel free to try it out, you should be able to put it onto any object and fly!

function Update () {
		
transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * 100,0,0);
transform.Rotate(0,Input.GetAxis("Horizontal") * Time.deltaTime * 100,0,Space.World);
transform.Rotate(0,0,-Input.GetAxis("Horizontal") * Time.deltaTime * 100);

//limit tilt
if (transform.eulerAngles.x >45  transform.eulerAngles.x<180)
transform.eulerAngles.x = 45;
else if (transform.eulerAngles.x<315  
transform.eulerAngles.x>180) transform.eulerAngles.x = 315;

if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
transform.eulerAngles.z = 45;
else if (transform.eulerAngles.z<315  
transform.eulerAngles.z>180) transform.eulerAngles.z = 315;




//return to 0 if nothing is pressed
if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,100 * Time.deltaTime);
if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-100 * Time.deltaTime);
if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);

if((transform.eulerAngles.x>0)  (transform.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.Rotate(100 * Time.deltaTime,0,0);
if((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.Rotate(0,0,0);
if((transform.eulerAngles.x>0)  (transform.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.Rotate(-100 * Time.deltaTime,0,0);
if(((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.Rotate(0,0,0);

//always forward
transform.Translate(0,0,20 * Time.deltaTime);


}

Not a bad script, you are correct, the way you have it, you are telling it to “adjust for the x rotation, then erase that, then adjust for the y rotation, then erase that then adjust for the z rotation”, thus erasing left and right when you complete it…

You should of course combine all three to get your new rotation. See the script below.

I tinkered with the code a bit I just like putting variables so that you can adjust things like speed and rotation.

var rotationRate=200;
var returnRotationRate=50;
var speed=50;

function Update () {

//transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,0,0);
//transform.Rotate(0,Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,0,Space.World);
//transform.Rotate(0,0,-Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);

transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,
	Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,
	-Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);

//limit tilt
if (transform.eulerAngles.x >45  transform.eulerAngles.x<180)
transform.eulerAngles.x = 45;
else if (transform.eulerAngles.x<315  
transform.eulerAngles.x>180) transform.eulerAngles.x = 315;

if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
transform.eulerAngles.z = 45;
else if (transform.eulerAngles.z<315  
transform.eulerAngles.z>180) transform.eulerAngles.z = 315;




//return to 0 if nothing is pressed
if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,returnRotationRate * Time.deltaTime);
if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-returnRotationRate * Time.deltaTime);
if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);

if((transform.eulerAngles.x>0)  (transform.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.Rotate(returnRotationRate * Time.deltaTime,0,0);
if((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.Rotate(0,0,0);
if((transform.eulerAngles.x>0)  (transform.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.Rotate(-returnRotationRate * Time.deltaTime,0,0);
if(((transform.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.Rotate(0,0,0);

//always forward
transform.Translate(0,0,speed * Time.deltaTime);


}

Ahh, I see, Thanks. I still gotta make it so that it doesn’t move down when you go side to side. Is there possibly a way to make only the y rotation world space? Hmm, anyone know the best way to keep it going only side to side but still rotate on the z axis when left or right is pressed?

I think the simplest way is to create a second object. That object would be the parent object of the one you are working on. That object would only receive the x and y rotations (turning the craft) where the ship would receive the z rotation giving the appearance of banking.

You will also need to adjust the transform orientations throughout the code to reflect the parenting.

I just hacked this code together without testing, there could be a couple of .parent’s that I missed.

var rotationRate=200;
var returnRotationRate=50;
var speed=50;

function Update () {

//transform.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,0,0);
//transform.Rotate(0,Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,0,Space.World);
//transform.Rotate(0,0,-Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);


transform.parent.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * rotationRate,
	Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,
	0);

transform.Rotate(0,
	Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate,
	-Input.GetAxis("Horizontal") * Time.deltaTime * rotationRate);

//limit tilt
if (transform.parent.eulerAngles.x >45  transform.parent.eulerAngles.x<180)
transform.parent.eulerAngles.x = 45;
else if (transform.parent.eulerAngles.x<315  
transform.parent.eulerAngles.x>180) transform.parent.eulerAngles.x = 315;

if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
transform.eulerAngles.z = 45;
else if (transform.eulerAngles.z<315  
transform.eulerAngles.z>180) transform.eulerAngles.z = 315;




//return to 0 if nothing is pressed
if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,returnRotationRate * Time.deltaTime);
if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-returnRotationRate * Time.deltaTime);
if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);

if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.Rotate(returnRotationRate * Time.deltaTime,0,0);
if((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(0,0,0);
if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(-returnRotationRate * Time.deltaTime,0,0);
if(((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.parent.Rotate(0,0,0);

//always forward
transform.parent.Translate(0,0,speed * Time.deltaTime);


}

your code seems to be more good if your spaceship has a character controller collider now my question would be is your ship a rigidbody? and if it is have you tried making your code using the rigidbody functions? such as AddTorque or AddForce it would make the code a lot smaller thus having physics for your ship.now for more about it you should go to the scripting reference for it and if for example your ship rotates in a way it does not supposed to use the configurable joint to lock those rotations and that is actually a component and not a code by the way. this is just an idea to try if you are getting problems sometimes there are situations in which you might have to end up using a diffeent aproach in your project.

There was no rigid body attached to the ship for that code, it had funky problems when I had attached it. I am sure the rigid body functions would do fine. I am working on a physics airplane.

Hey MisterB, checked it out a little, I got some funky stuff going on with the parent stuff but so I ended up applying a separate script to the parent object and it worked, I think I just missed some stuff though. It’s late now, so I’ll be giving it another shot next chance I get. I got it nearly work fine, the only little thing is if your flying down, and you press left/right, and turn all the way in that direction, then let go of the left button and press the right button you will stop turning left entirely and continue to dive down, but you wont turn right, you have to let the ship level out a little before you can continue to dive down and turn right, which would be a pretty frustrating situation I think. Like I said I wanna try to get it all into one script, if I do that and I’m still having trouble I post it on here.

Foxter, no rigid body on here, I messed around with addtorque though, it looked pretty cool, I be sure to check em out in the future, thanks for the input.

Okay here’s what I got, it all works great except one little thing, if you go up or down AND left or right the parent eventually rotates and starts acting weird, however if you only go up or down, or only go left or right, then it’s stays fine. Here’s the script

var returnRotationRate = 50;
function Update () {

transform.Rotate(0, 0,-Input.GetAxis("Horizontal") * Time.deltaTime * 100);

//but dont rotate too much
if (transform.eulerAngles.z >45  transform.eulerAngles.z<180)
transform.eulerAngles.z = 45;
else if (transform.eulerAngles.z<315  
transform.eulerAngles.z>180) transform.eulerAngles.z = 315;


//stop rotation if nothing is pressed
	if((transform.eulerAngles.z>0)  (transform.eulerAngles.z>180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,returnRotationRate * Time.deltaTime);
if((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,0);
if((transform.eulerAngles.z>0)  (transform.eulerAngles.z<180)  (!Input.GetAxis("Horizontal"))) transform.Rotate(0,0,-returnRotationRate * Time.deltaTime);
if(((transform.eulerAngles.z == 0)  (!Input.GetAxis("Horizontal")))) transform.Rotate(0,0,0);



//parent code
transform.parent.Rotate(Input.GetAxis("Vertical") * Time.deltaTime * 100,0,0);
transform.parent.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * 100,0,Space.World);


//limit parent rotation


if (transform.parent.eulerAngles.x >45  transform.parent.eulerAngles.x<180)
transform.parent.eulerAngles.x = 45;
else if (transform.parent.eulerAngles.x<315  
transform.parent.eulerAngles.x>180) transform.parent.eulerAngles.x = 315;

	if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x>180)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(returnRotationRate * Time.deltaTime,0,0);
if((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(0,0,0);
if((transform.parent.eulerAngles.x>0)  (transform.parent.eulerAngles.x<180)  (!Input.GetAxis("Vertical"))) transform.parent.Rotate(-returnRotationRate * Time.deltaTime,0,0);
if(((transform.parent.eulerAngles.x == 0)  (!Input.GetAxis("Vertical")))) transform.parent.Rotate(0,0,0);

transform.parent.Translate(0,0, Time.deltaTime * 50);


}

it seams like it has to do with the

if (transform.parent.eulerAngles.x >45  transform.parent.eulerAngles.x<180)
transform.parent.eulerAngles.x = 45;
else if (transform.parent.eulerAngles.x<315  
transform.parent.eulerAngles.x>180) transform.parent.eulerAngles.x = 315;

But that bit of code was suggested to me, so I don’t understand it fully. Anyone got any advice I feel like I’m very close with this and I would love some advice, thanks.

Bump.

could you show an example of what you are trying to do? at the moment…for me it just looks like a body rotates some degrees on it´s own axis … :wink: should it behave like a spaceship or more like an airplane?

The ship itself is parented to a cube, and the ship rotates on the x and y axis around the parent cube so that it can rotate on the z axis to simulate banking, I believe it’s called. It is supposed to be a Starfox style space ship, so it should behave like a spacecraft. And it always has a forward momentum, which is one of the last parts of the script.
Does that help, or should I figure out how to upload the file? :stuck_out_tongue:

Okay this is embarrassing, I want to upload the build but I never have before, I tried attaching as an attachment but that didn’t work, could anyone direct me in the proper manner for this.