- Home /
Question by
Sercon · Oct 21, 2020 at 02:31 PM ·
camerascripting problemcamera-movementcamera rotation
Why is the camera movement framerate dependent after adding deltaTime?
So I am using a camera controller with the new input system. But I keep having this issue that the camera is dependent on the framrate. It drastically accelerates the lower the FPS. Now I did add deltaTime in the calculations but it didn't do anything. Does anyone have an idea what is going wrong here? Much appreciate any help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using Cinemachine;
using AU3D.Controls.Inputs;
public class PlayerCameraController : NetworkBehaviour
{
[Header("Camera")]
[SerializeField] private Vector2 maxFollowOffset = new Vector2(-1f, 6f);
[SerializeField] private Vector2 cameraVelocity = new Vector2(4f, 0.25f);
[SerializeField] private Transform playerTransform = null;
[SerializeField] private CinemachineVirtualCamera virtualCamera = null;
[SerializeField] private bool canLook = true;
private Controls controls;
private Controls Controls
{
get
{
if(controls != null) { return controls;}
return controls = new Controls();
}
}
private CinemachineTransposer transposer;
public override void OnStartAuthority()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
transposer = virtualCamera.GetCinemachineComponent<CinemachineTransposer>();
virtualCamera.gameObject.SetActive(true);
enabled = true;
Controls.Player.Look.performed += ctx => Look(ctx.ReadValue<Vector2>());
}
[ClientCallback]
private void OnEnable() => Controls.Enable();
[ClientCallback]
private void OnDisable() => Controls.Disable();
private void Look(Vector2 lookAxis)
{
float TimeDelta = Time.deltaTime;
if(canLook)
{
transposer.m_FollowOffset.y = Mathf.Clamp(
transposer.m_FollowOffset.y - (lookAxis.y * cameraVelocity.y * TimeDelta),
maxFollowOffset.x,
maxFollowOffset.y);
playerTransform.Rotate(0f, lookAxis.x * cameraVelocity.x * TimeDelta, 0f);
}
}
public void ToggleLook(bool state)
{
canLook = state;
}
}
Comment