- Home /
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!
If you want the angle to be (31, 0, 0), wouldn't you just write that? You seem to be using the y and x euler angles from another object.
Try creating a cube of rotation (31,0,0) and linking it to the script and using its rotation lightObj.transform.rotation = Cube.transform.rotation; and see if it works or not
I seem to have figured this out myself. I could just use
lightObj.transform.Rotate(31,0,0);
lightObj.transform.Rotate(-31,0,0);
But now im trying to make this a smooth rotation, so could you please go to my post and help as much as you can on that topic? because im nearly ripping my hear out because of smooth rotation.
Answer by aldonaletto · Aug 02, 2012 at 02:09 AM
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; } } }
it still doesnt seem to work. im getting values like "6.54845e-05"
6.54845e-05 is close to 0, and what you can expect with eulerAngles, given floating point precision issues.
As @Eric5h5 said, if the angles Y and Z are always zero you could just assign the desired angles to the object rotation:
lightObj.transform.eulerAngles = Vector3(31,0,0); ... lightObj.transform.eulerAngles = Vector3(0,0,0); ...
Yeah but when i activate the rotation, it rotates to the right with that code. the light just really glitches up.
The code you've posted (with the suggested corrections) should rotate down the lightObj 31 degrees instantly, wait 8 seconds and rotate it back instantly to 0 degrees, unless the original Update function does something else that nulls this behaviour - it seems you've not posted the complete Update function, because its closing bracket is missing.
Answer by MKlegoman357 · Aug 02, 2012 at 11:10 PM
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);
I seem to have figured this out myself. I could just use
lightObj.transform.Rotate(31,0,0);
lightObj.transform.Rotate(-31,0,0);
But now im trying to make this a smooth rotation, so could you please go to my post and help as much as you can on that topic? because im nearly ripping my hear out because of smooth rotation.
Answer by MKlegoman357 · Aug 03, 2012 at 08:27 AM
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.
Okay, so when i play the game the flashlight seems to only point in the direction that it was pointing when the game started. even twhen i turn it still continues to stay at the original rotation/position it started in. Its attached to the First person controllers main camera, so it should move when i turn. but it doesnt.
Your answer
Follow this Question
Related Questions
Euler to Quaternion conversion returns unexpected result at exactly 180 degree rotation on Y axis 0 Answers
Object won't rotate 2 Answers
Limiting the rotation of an object 1 Answer
Unity GUI Button Options 0 Answers
Rotation losing precision 0 Answers