How to rotate a certain degree

I’m trying to make a spaceship control script, at the moment if you hold down the up or down arrow the ship does a loop-de-loop, I obviously don’t want that, I want it to lean about 30 (or so) degrees up or down repetitively. I’ve tried a few different thing and I’m pretty desperate at the moment (I’m still a beginner ultimately) Here’s my script

var speed : float = 20.0;
var rotateSpeed : float = 20.0;
var target : Transform;
var stop : boolean;

function Update () {
var controller : CharacterController = GetComponent(CharacterController);
//you can go up or down
transform.Translate(Input.GetAxis("Horizontal") *Time.deltaTime * 1, Input.GetAxis("Vertical") * Time.deltaTime * 0, 0);

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


//lean left right, up down 
if(stop == false){
if(Input.GetKey(KeyCode.D)){
transform.Rotate(transform.up * Time.deltaTime * rotateSpeed, Space.World);
}
}

if(stop == false){
if(Input.GetKey(KeyCode.A)){
transform.localEulerAngles = Vector3(0,-30,0);
}
else {
if (Input.GetKeyUp(KeyCode.A))
transform.localEulerAngles = Vector3(0,0,0);
}

}

if(Input.GetKey(KeyCode.W))
transform.localEulerAngles = Vector3(30,0,0);
else {
if (Input.GetKeyUp(KeyCode.W))
transform.localEulerAngles = Vector3(0,0,0);
}

if(Input.GetKey(KeyCode.S))
transform.localEulerAngles = Vector3(-30,0,0);
else {
if(Input.GetKeyUp(KeyCode.S))
transform.localEulerAngles = Vector3(0,0,0);
}
}

Furthermore I’ve been thing about making a min distance max distance var for all directions ( I saw this on unity answers some where) but that semas dificult and like it may be buggy. I’d LOVE some help.

I did something like this for my flight sim (with help from the forum);

if (transform.eulerAngles.z >65  transform.eulerAngles.z<180) transform.eulerAngles.z = 65;
else if (transform.eulerAngles.z<295  transform.eulerAngles.z>180) transform.eulerAngles.z = 295;

This effectively clamps the angle between whatever euler angles you choose and prevents rollover of the craft (which was my problem as well)

You can try my sim at this link if you like. It’s crude still, but it works.

Thanks for the reply!

First off, that’s a tight looking flight sim, let me know when you finish that one :slight_smile:

the script was very helpful!

At the moment I have one question; is there a way to make eulerAngles not pop the object to the position? I think it’s because it’s just moving to quickly, unless eulerAngles are like transform.position…are they? I tried doing "transform.eulerAngles.z (30 * Time.deltaTime * 5) But whenever I apply any kind of Time.deltaTime the ship acts funny, I think it’s called clipping? Where it kind of…buzzes, I guess you could say. How can I fix this?

I think, from what I see, your set up is off a bit. Look at this link in the scripting manual. They don’t recommend using transform.eulerangles.z for anything but reading or setting absolute values. You might try Mathf.LerpAngle. I haven’t used this particular function, but it seems like it might help. Or else use the slerp function (which I have used often. The missiles in my sim use it). Perhaps someone else can give a clearer answer. My flight sim is physics based. I use AddTorque to do the pitch and yaw rotations and AddForce for thrust inside a FixedUpdate function, so I didn’t have to deal with rotating mathematically. I let the physics engine do the heavy lifting.

Seems like with what you’re doing, a simple transform.Rotate (0,0,Time.deltaTime* rollfactor) ought to work with the angles clamped as shown above (rollfactor is just a variable to allow you to adjust the roll speed). The only thing is that when it hits the clamp point, it will stop rotating abruptly. That’s why I used physics. I found the natural damping more realistic looking, although I’m sure you could apply damping with the Mathf.Damp function somehow. I’m not that familiar with it.

I realized a bit later how to use it with transform.Rotate. I’ll certainly check out the Mathf.LerAngle. And hey, would that script you gave me work for having the ship rotate back to 0,0,0 when nothing is being pressed, currently I still have my old setup with just the euler angle, here’s the script.

//lean left right, up down 

if(Input.GetKey(KeyCode.D)){
transform.Rotate (0,0,-30 * Time.deltaTime);
if (transform.eulerAngles.z >30  transform.eulerAngles.z<180)
transform.eulerAngles.z = 30;
else if (transform.eulerAngles.z<345  
transform.eulerAngles.z>180) transform.eulerAngles.z = 345;



}
else{
if(Input.GetKeyUp(KeyCode.D))
transform.eulerAngles.z = 0;

}

if(Input.GetKey(KeyCode.A)){
transform.Rotate(0,0,30 * Time.deltaTime);
if (transform.eulerAngles.z >10  transform.eulerAngles.z<180)
transform.eulerAngles.z = 10;
else if (transform.eulerAngles.z<-345  
transform.eulerAngles.z>-180) transform.eulerAngles.z = -345;

}
else {
if (Input.GetKeyUp(KeyCode.A))
transform.eulerAngles.z = 0;


}



if(Input.GetKey(KeyCode.W)) {
transform.Rotate(30 * Time.deltaTime,0,0);
if (transform.eulerAngles.x >30  transform.eulerAngles.x<180)
transform.eulerAngles.x = 30;
else if (transform.eulerAngles.x<345  
transform.eulerAngles.x>180) transform.eulerAngles.x = 345;
}

else {
if (Input.GetKeyUp(KeyCode.W))
transform.eulerAngles.x = 0;
}

if(Input.GetKey(KeyCode.S)){
transform.Rotate (-30 * Time.deltaTime,0,0);
if (transform.eulerAngles.x >30  transform.eulerAngles.x<180)
transform.eulerAngles.x = 30;
else if (transform.eulerAngles.x<345  
transform.eulerAngles.x>180) transform.eulerAngles.x = 345;
}
else {
if(Input.GetKeyUp(KeyCode.S))
transform.eulerAngles.x= 0;

}
}

I think you just need to modify your else statement.

else if (Input.GetKeyUp(KeyCode.S))
{
if ( transform.eulerAngles.x > 180  transform.eulerAngles.x <360 )
{
transform.Rotate (Time.deltaTime*rotationfactor, 0,0)
}
else if (transform.eulerAngles < 180  transform.eulerAngles.x >0)
{
transform.Rotate(Time.deltaTime*-rotationfactor,0,0)
}
}

I haven’t tested this and I’m far from being an expert coder, so someone else may have better ideas, but perhaps this could be a starting point. Let me know what you come up with.