I have this stupidly weird problem. I wrote the code down bellow and it was working fine till some point… For some reason now Unity (180, 180, 0) and (0, 180, 0) rotates Z AXIS INSTEAD OF X, always. What is this weird sh*t that Unity is pulling off on me? This thing is making me so frustrated and I’m banging my head against the wall, why me…
Thanks for any help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotateUSB : MonoBehaviour {
private GameObject USB;
gameVariables GameVariables = new gameVariables();
private void Start() {
USB = this.gameObject;
}
public void inverseUSBrotation() {
if (USB.transform.rotation.x > 90 && USB.transform.rotation.x < 270) {
USB.transform.Rotate(0f, -180f, 0f, Space.Self);
GameVariables.UsbCorrectRotation = 1;
} else {
USB.transform.Rotate(180f, -180f, 0f, Space.Self);
GameVariables.UsbCorrectRotation = 0;
}
}
}
Does a parent transform of USB object have rotation? It might have happened accidentally… probably worth a look at each parent Transform in the inspector to make sure.
I checked the video, I think it’s not that, I tried moving and rotating the object, but still doesn’t work. Before, when I was working, the object was at the exact same position, so that can’t be, right?
if (USB.transform.rotation.x > 90 && USB.transform.rotation.x < 270) {
Rotations in Unity are represented by Quaternions. They are 4-dimensional numbers and each (x, y, z, w) range from 0 to 1, and they don’t correspond directly to each axis of rotation. You can’t just read the ‘x’ part of the quaternion and expect to get degrees out of it.
You might be able to get away with inspecting transform.rotation.eulerAngles.x instead.
I know what you mean, but that is the weirdest part… Even if I eliminate whole code and make it look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotateUSB : MonoBehaviour {
private GameObject USB;
gameVariables GameVariables = new gameVariables();
private void Start() {
USB = this.gameObject;
}
public void inverseUSBrotation() {
USB.transform.Rotate(180f, -180f, 0f, Space.Self);
}
}
THE PROGRAM WORKS LITERALLY THE SAME
It rotates the wrong axis and what’s even more creepy, the IF statement still works, I can rotate it and rotate it back, even where there is no if statement that would do this. But if I eliminate that last part, the program will stop working. I tried to even make a new project… Nothing
Well yeah of course it does. Due to the way Quaternions work your if statement was never going to return true. And since you are rotating by 180 degrees on each of those axes, of course doing it twice will rotate the object back to where you started.
transform.Rotate is a relative rotation. It adds the rotation you give it to the existing rotation of the object. Mind sharing some screenshots of the object and its inspector to show the behaviour you’re seeing?
Via canvas, button, script is on the USB object, USB object dragged to the button and chosen the function inverseUSBrotation… In the past 3 years, I always did that