Home > Blockchain >  Unity camera rotation script shaking camera
Unity camera rotation script shaking camera

Time:08-25

I am trying to make a camera that can be rotated by using the mouse, but for some reason the camera jitters and shakes when I move the mouse. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.RuleTile.TilingRuleOutput;

public class rotatecamera : MonoBehaviour
{
    public GameObject camera;
    public float turnSpeed = 4.0f;
    private float rotX;
    private void Update()
    {

        rotX = Input.GetAxis("Horizontal") * turnSpeed;
        camera.transform.localEulerAngles = new Vector3(Input.GetAxis("Mouse X") * turnSpeed, 0, 0);
    }
    
}

CodePudding user response:

You are rotating by 4 degrees per frame.

The frame rate will depend on your FPS, but it is probably going to be at least 60.

Thus you will be rotating up to 240 degrees per second. That's probably not what you want.

You should use

 new Vector3(Input.GetAxis("Mouse X") * turnSpeed * Time.deltaTime, 0, 0);

Time.deltaTime will give you the elapsed time since the previous frame. This will give a rate of rotation up to 4 degrees per second.

You might also want to consider using FixedUpdate where the update rate is fixed and use Time.fixedDeltaTime.

CodePudding user response:

My camera still isn't working:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.RuleTile.TilingRuleOutput;

public class rotatecamera : MonoBehaviour
{
    public GameObject camera;
    public float turnSpeed = 4.0f;
    private float rotX;
    void Update()
    {
        Debug.Log(Input.GetAxis("Mouse X"));
        gameObject.transform.eulerAngles = new Vector3(0, Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime, 0);
    }
    
}

  • Related