- Home /
if the fps is high the camera rotate much faster
if the fps is high the camera rotate much faster. how can I make sure that it does not matter what fps you have and it does not rotate faster or slower
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraControl : MonoBehaviour
{
public Slider zoomSlider;
private const float Y_ANGLE_MIN = 1.0f;
private const float Y_ANGLE_MAX = 60.0f;
public Transform lookAt;
public Transform camTransform;
public float distance = 5f;
private float currentX = 0.0f;
private float currentY = 45.0f;
public Joystick joystick;
public bool joystickEnabled = true;
private void Start()
{
camTransform = transform;
}
private void Update()
{
if (joystickEnabled == false)
{
currentX += Input.GetAxis("Horizontal");
currentY -= Input.GetAxis("Vertical");
}
else
if (joystickEnabled == true)
{
currentX += joystick.Horizontal;
currentY -= joystick.Vertical;
}
currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt(lookAt.position);
}
public void Zoom()
{
distance = zoomSlider.value;
}
}
Answer by TreyH · May 08, 2018 at 08:14 PM
Well, you aren't normalizing anything by time. Update and LateUpdate will run as fast as the client machine can process frames. As a result, you'll want to make sure there's a time-dependent coefficient on any sort of movement happening there:
if (joystickEnabled == false)
{
currentX += Input.GetAxis("Horizontal") * Time.deltaTime;
currentY -= Input.GetAxis("Vertical") * Time.deltaTime;
}
else
{
currentX += joystick.Horizontal * Time.deltaTime;
currentY -= joystick.Vertical * Time.deltaTime;
}
When you first implement this, your rotation will be much slower than it was. People typically have an additional coefficient to tune how quickly the now-normalized rotation occurs.
Also don't forget that if you manipulate the timescale or the speed of the game the time.deltatime will be affected too. To avoid this use time.fixedDeltatime
If Time.timeScale has been adjusted and you still wish to rotate at a fixed rate regardless of that timeScale, you can use it as a modifier of that rate.
if(Time.timeScale > 0) // Divide-by-zero safety
{
float rotationRateX = (sensitivityX * Input.GetAxis("Horizontal") * Time.deltaTime) / Time.timeScale;
}
By extension, when using $$anonymous$$ouse movement for input, you likely wouldn't want to scale the result by time in any manner.
Time.fixedDeltaTime is actually for the physics cycle and FixedUpdate. To account for scaled time, you can just use Time.unscaledDeltaTime.
Ty for the re$$anonymous$$ders, scaled time slipped my $$anonymous$$d.