Rotation Code Help

Whats wrong with this code? Its supposed to make the object rotate to (31, 0, 0) when left shift is pressed and then when left shift is released it makes it go back to (0, 0, 0) rotation. Can someone help?

This isnt a question on the sprint script, its a question on the rotation. The rotation goes crazy when you press left shift.

function Update(){
 if(Input.GetButtonDown("Sprint") && isSprinting == false && canRun == true){
 
 obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 8;
 
 lightObj.transform.eulerAngles = Vector3(31, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);
 
 isSprinting = true;
 
 StopSprinting();

}

function StopSprinting(){

 yield WaitForSeconds (8);

 if(isSprinting == true){
 
 isSprinting = false;
 
 lightObj.transform.eulerAngles = Vector3(0, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);
 
 audio.PlayOneShot(breathSound);
 
 audio.volume = 1;
 
 obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 6;
 
 canRun = false;
 
 yield WaitForSeconds (10);
 
 if(canRun == false){
 
 canRun = true;
 
 }
 }
}

Please help!

You’re swapping X and Z angles each time you rotate to 31 or return to 0:

  // the last eulerAngles component should be Z, not X:
  lightObj.transform.eulerAngles = Vector3(31, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);
  ...
  // the same applies to this line:
  lightObj.transform.eulerAngles = Vector3(0, lightObj.transform.eulerAngles.y, lightObj.transform.eulerAngles.x);

Change the last component to lightObj.transform.eulerAngles.z

EDITED: If you want to rotate smoothly to the new orientation, have a variable (lightAngle) to define the desired angle and follow it at Update with Lerp:

// declare these variables in your script:
var lightVel: float = 5; // speed to rotate the lightObj
private var curAngle: Vector3; // current lightObj local angle
private var lightAngle: Vector3; // desired lightObj local angle

function Start(){
  // initialize the new variables at Start:
  lightAngle = lightObj.localEulerAngles;
  curAngle = lightAngle;
  // other previous Start code
}
  
function Update(){
  if(Input.GetButtonDown("Sprint") && isSprinting == false && canRun == true){
    obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 8;
    lightAngle.x = 31; // set the desired angle
    isSprinting = true;
    StopSprinting();
  }
  // this code goes smoothly to the desired angle:
  curAngle = Vector3.Lerp(curAngle, lightAngle, lightVel * Time.deltaTime);
  lightObj.localEulerAngles = curAngle;
  // other previous Update code
}

function StopSprinting(){
  yield WaitForSeconds (8);
  if(isSprinting == true){
    isSprinting = false;
    lightAngle.x = 0; // restore the original light angle
    audio.PlayOneShot(breathSound);
    audio.volume = 1;
    obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 6;
    canRun = false;
    yield WaitForSeconds (10);
    if(canRun == false){
      canRun = true;
    }
  }
}

Try to use this:

lightObj.transform.localRotation.x = 31;

lightObj.transform.localRotation.x = 0;

or

lightObj.transform.localRotation = Quaternion.Euler(31, lightObj.transform.localRotation.y, lightObj.transform.localRotation.z);

lightObj.transform.localRotation = Quaternion.Euler(0, lightObj.transform.localRotation.y, lightObj.transform.localRotation.z);

This should work:

var xRotation : float;

var defaultRotation : Quaternion;

var xRotationV : float;

var xRotationTime : float = 1;

function Start ()
{

defaultRotation = transform.localRotation;

xRotation = defaultRotation.x;

}

function Update()
{
lightObj.transform.rotation = Quaternion.Euler(Mathf.SmoothDamp(transform.localRotation.x, xRotation, xRotationV, xRotationTime), defaultRotation.y, defaultRotation.z);

if(Input.GetButtonDown(“Sprint”) && isSprinting == false && canRun == true){

obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 8;

isSprinting = true;

xRotation = 31;

StopSprinting();

}
}

function StopSprinting()
{

yield WaitForSeconds (8);

if(isSprinting == true){

isSprinting = false;

xRotation = 0;

audio.PlayOneShot(breathSound);

audio.volume = 1;

obj1.GetComponent(CharacterMotor).movement.maxForwardSpeed = 6;

canRun = false;

yield WaitForSeconds (10);

if(canRun == false){

canRun = true;

}
}
}

If its not then try defaultRotation declair by your self in the inspector.