- Home /
Problem with gameObject.transfrom.rotation and GetAxis
I'm trying to make a turning script that both moves and rotates an object, though it's not quite working as intetended`
if (Input.GetAxis ("Horizontal") > 0) {
objectPostition.x += turnSpeed * Time.deltaTime;
if (gameObject.transform.rotation.x != -45.0f) {
Debug.Log ("Called");
Debug.Log (objectRotation.x);
objectRotation.x -= tilt * Time.deltaTime;
if (objectRotation.x < -45.0f) {
objectRotation.x = -45.0f;
}
} else if (Input.GetAxis ("Horizontal") < 0) {
objectPostition.x -= turnSpeed * Time.deltaTime;
if (gameObject.transform.rotation.x != 45.0f) {
Debug.Log ("Called");
objectRotation.x += tilt * Time.deltaTime;
if (objectRotation.x > 45.0f) {
objectRotation.x = 45.0f;
}
}
if (gameObject.transform.rotation.x != 0.0f) {
Debug.Log ("Called");
if (gameObject.transform.rotation.x <= 45.0f) {
Debug.Log ("Called");
objectRotation.x -= tilt * Time.deltaTime;
} else if (gameObject.transform.rotation.x >= -45.0f) {
Debug.Log ("Called");
objectRotation.x += tilt * Time.deltaTime;
}
}
}
}`
This is the main block of code called within Update().
ObjectRotation is a Quaternion variable I've created which is meant to update the object's rotation in Update():
gameObject.transform.rotation = objectRotation;
However, when an input is entered, the object's X Rotation changes to either 180 or -180(Depending on the Axis) I have no idea what's causing this, but I think the problem is that transform.rotation is not updating, considering I also made this if statement, in the hopes of forcefully updating the value, but it also doesn't work, but logs the message into the console:
if (Input.GetKeyDown (KeyCode.R)) {
gameObject.transform.rotation = objectRotation;
Debug.Log ("Pressed");
}
There are also no errors in the script or that occur during runtime.
Your answer
Follow this Question
Related Questions
Rotate camera on mouse drag, minor glitch 1 Answer
Rotation 90.0001 and 1.001719 bugs! 1 Answer
Avoiding Gimbal Lock 1 Answer
Get rotation from a vector direction. 1 Answer
Align GameObject to Terrain angle 2 Answers