- Home /
Local Euler Angles not Rotating GameObjects
Hello all! I am having trouble getting text to follow alongside my weapon for my VR game. I would just set it as a child of the gun, but this causes the text to lag behind the gun when the gun is moved. Other VR devs recommended updating the text's position and rotation to that of the gun every frame, but for some reason when I try using the code below, the text follows the gun's position correctly but does not rotate at all. Any help is appreciated. Thank y'all so much!
ammoUI.transform.position = new Vector3 (this.transform.parent.position.x+0.02f, this.transform.parent.position.y+0.02f, this.transform.parent.position.z+0.02f);
ammoUI.transform.localEulerAngles = new Vector3 (this.transform.parent.rotation.z, this.transform.parent.rotation.x, this.transform.parent.rotation.y);
healthUI.transform.position = new Vector3 (this.transform.parent.position.x+0.02f, this.transform.parent.position.y+0.02f, this.transform.parent.position.z+0.02f);
healthUI.transform.localEulerAngles = new Vector3 (this.transform.parent.rotation.z, this.transform.parent.rotation.x, this.transform.parent.rotation.y);
Answer by Bunny83 · Oct 15, 2017 at 10:55 PM
Those two lines of code make no sense:
ammoUI.transform.localEulerAngles = new Vector3 (this.transform.parent.rotation.z, this.transform.parent.rotation.x, this.transform.parent.rotation.y);
healthUI.transform.localEulerAngles = new Vector3 (this.transform.parent.rotation.z, this.transform.parent.rotation.x, this.transform.parent.rotation.y);
First of all you treat the x, y and z component of a Quaternion like it's euler angles which they aren't. A Quaterion is a 4 dimensional complex number and each component is in the range -1 to 1. If you don't know what a Quaternion is you shouldn't touch it's x, y, z and w component.
Next thing is even when you would use the euler angles of the parents rotation it wouldn't make much sense to assign the localEulerAngles to them. The localEulerAngles are relative to the paremt object. So setting the worldspace rotation of the parent as local rotation is nonsense. The result would be some random rotation.
If you want to set the objects position and rotation the same as the parent of the object where the script is attached to you just assign the position and rotation one-to-one.
ammoUI.transform.rotation = transform.parent.rotation;
healthUI.transform.rotation = transform.parent.rotation;
Note that your position lines also seems a bit weird. The position is in worldspace. Adding an offset to the position will add this offset in worlspace. This might make sense if you want it to appear above the object, but offsetting it in x and z doesn't seem to make a lot of sense. Also the line would be more readable this way:
ammoUI.transform.position = transform.parent.position + new Vector3(0.02f, 0.02f, 0.02f);
Your answer
![](https://koobas.hobune.stream/wayback/20220612141119im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Tilting with transform.localEulerAngles and Vector3.Lerp not working. 1 Answer
Multiple Cars not working 1 Answer
C# Creating a 2d boundary 1 Answer