- Home /
character controller rotation won't accept script values
Hi,
I am trying to set the character controller rotation by script so that the controller will move along that angle on play. I can script the values OK, but when I play the scene, the x and z values of the character controller's rotation always revert back to 0. Is there any way to work around this? Example code:
var rot : Quaternion = Quaternion.LookRotation(dir);
this.transform.rotation = rot;
Same thing happens if I set the rotation manually:
this.transform.eulerAngles = new Vector3(10, 45, 150);
On play, the Y value will be 45, but the x and z will revert to zero. Any help will be greatly appreciated.
Answer by Zaffer · Jul 30, 2014 at 02:59 PM
I discovered the problem. It's in the MouseLook script, specifically, the line:
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
I don't really understand why, but this code causes the X value of the object to revert to zero when play starts, overriding any scripted value for X. (The Z value is not a factor as that is constant at zero.) I found a workaround by commenting out all the code in the MouseXAndY Axes choice and substituting this one line of code which does not use EulerAngles.
transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityY, Input.GetAxis("Mouse X") * sensitivityX, 0);
Zaffer
Answer by ado112 · Jul 29, 2014 at 04:57 PM
Have you tried to use transform.rotation?
Thanks, but I thought that was what I was doing with the line:
this.transform.rotation = rot;
transform.rotation needs a Quaternion, transform.eulerAngles takes a Vector3 number. As far as I can tell, the character controller ignores x and z values for both. Anybody else have a suggestion?
i mean: transform.rotation.x = 10; transform.rotation.z = 150;
Thanks Ado, I tried that, but same result, x and z were reset to zero :-(
Your answer
![](https://koobas.hobune.stream/wayback/20220613155315im_/https://answers.unity.com/themes/thub/images/avi.jpg)