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.

,

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 :slight_smile:

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