How can I limit rotation on y axis then have it return to orignal position when key is not pressed?

Hello…Im new to unity and this community so my questions might be “noobish” to say the least. Im working on a top down shooter game. Im not too deep into the project yet as I am trying to get basic movement scripted. As of now, my ship can move up,down, left and right…but when I move left and right I want the ship to tilt(rotate) on Y axis a limited amount, then rotate back to the original position when the key isnt pressed. Im looking for help on how to do it, and how to incorporate it my existing code. Heres my code so far.

var Speed : int = 5;

function Update () 

{
     if(Input.GetButton("left")) {
     
     transform.Translate(Vector3(-Speed,0,0) * Time.deltaTime);
         
    }
    
     if(Input.GetButton("right")) {
     
     transform.Translate(Vector3(Speed,0,0) * Time.deltaTime);
          
    }
    
     if(Input.GetButton("up")) {
     
     transform.Translate(Vector3(0, 0, Speed) * Time.deltaTime);
          
    }
    
     if(Input.GetButton("down")) {
     
     transform.Translate(Vector3(0, 0, -Speed) * Time.deltaTime);
          
    }
    
       //Check border bounds
     
     if(transform.position.y > 44)
     {
          transform.position.y = 44;
     }
    
     if(transform.position.y < -44)
     {
          transform.position.y = -44;
     }
     
        if(transform.position.x > 62)
     {
          transform.position.x = 62;
     }
     
        if(transform.position.x < -62)
     {
          transform.position.x = -62;
     }    




}

var Speed : int = 5;
var oldRot : Vector3;

function Update () 
 
{
     if(Input.GetButton("left")) {
     oldRot = Transform.localEulerAngles;
     transform.Translate(Vector3(-Speed,0,0) * Time.deltaTime);
     transform.localEulerAngles(45,0,0);
 
    }
    

    if(Input.GetButtonUp("left") || Input.GetButtonUp("right") || Input.GetButtonUp("up") || Input.GetButtonUp("down"))
    {
         transform.localEulerAngles(oldRot.x,oldRot.y,oldRot.z);
    }

This isn’t exactly graceful, but it should work. The angle (45) may not work for you, I don’t know how your ships are oriented in unity. But you can rotate them inside the editor to see what looks good and which axis you wanted.

If the game’s camera is fixed, I would just translate according to the world’s axis since it’s a top down. That way you can tilt your ship however without affecting the translate. To translate according to the world you just add ‘Space.World’ for each translate.

As for the tilting, just set the eulerAngles on your forward axis (normally ‘z’) to the angle you want to tilt. So I think it would look something like this.

if(Input.GetButton("left")) {
 
transform.Translate(Vector3(-Speed,0,0) * Time.deltaTime, Space.World);
transform.eulerAngles = Vector3(0,0,45);
 
}
 
if(Input.GetButton("right")) {
 
transform.Translate(Vector3(Speed,0,0) * Time.deltaTime, Space.World);
transform.eulerAngles = Vector3(0,0,-45);
}

This won’t interpolate smoothly. It’ll just snap to that angle. Than in update, just add this…

if(Input.GetButtonUp("right") || Input.GetButtonUp("left")){
    	transform.eulerAngles = Vector3.zero;
    }

…to snap back. There is ways to interpolate smoothly with lerps, but this is a good starting point.