- Home /
Gameobjects rotation goes haywire,
public class Script : MonoBehaviour
{
//The cam script
[SerializeField]
cam Cam;
[SerializeField]
Transform arms;
float xrotation = 90f;
// Update is called once per frame
void Update()
{
xrotation += Cam.xRotation;
transform.localEulerAngles = new Vector3(xrotation, -90, 0);
}
}
I have a problem my xrotation goes crazy if I look way up or way down. And I know why its happening, I think its because this (xrotation += Cam.xRotation;) is inside void Update and it keeps adding the value of Cam.xRotation to the xrotation float every frame and the sum of that answer gets to be the new xrotation value and it goes on again and again. Now the question is, how can I add those 2 floats but not adding it again and again? Just once when the Cam.xRotation value is changed.
,
$$anonymous$$aybe try doing a comparison with a buffer if the value has significantly changed or not? say:
if(new rotation > last rotation + buffer) { perform rotation; } else last rotation = new rotation;
There might be a better way but I'm not sure how you're trying to use your camera (FPS, 3rd person, custom system, etc). I always mix up assigning a new rotation v. using the Rotate function that happens every update. Also check out cine$$anonymous$$achine plugin - maybe that's what you're looking for
Answer by fblast1 · May 07, 2020 at 02:17 PM
Well,try this
//The cam script
[SerializeField]
cam Cam;
[SerializeField]
Transform arms;
float xrotation = 90f;
int tempValue;
// Update is called once per frame
private void Start()
{
tempValue = Mathf.Round(Cam.xRotation);
}
void Update()
{
if(Mathf.Round(Cam.xRotation) != tempValue)
{
tempValue = Mathf.Round(Cam.xRotation);
xrotation += Cam.xRotation;
transform.Rotate(xrotation, -90, 0);
}
//transform.localEulerAngles = new Vector3(xrotation, -90, 0);
}
Well I didn't test it since you are using other script that is involved :)
The idea is that you round that value to int number then you assign it to a variable then check if it is equals to it or not if not you add that value to your xrotation .
Regards
Your answer
![](https://koobas.hobune.stream/wayback/20220612233446im_/https://answers.unity.com/themes/thub/images/avi.jpg)