Camera movement feels weird
I am creating a camera control script, it works fine but when moving horizontally it feels like it slows sometimes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour
{
public Transform player;
public float distance = 3f;
public float sensitivity = 10f;
private Vector3 offset;
private float playerHeight;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
if (player)
{
playerHeight = player.GetComponent<PlayerControl>().height;
offset = new Vector3(player.position.x, player.position.y + playerHeight, player.position.z - distance);
}
}
private void LateUpdate()
{
if (player)
{
Vector3 xOffset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensitivity, Vector3.up) * offset;
Vector3 yOffset = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensitivity, Vector3.right) * offset;
if (yOffset.y > playerHeight + distance)
{
yOffset.y = playerHeight + distance;
}
else if (yOffset.y < 0)
{
yOffset.y = 0;
}
offset = new Vector3(xOffset.x, yOffset.y, xOffset.z);
transform.position = player.position + offset;
transform.LookAt(player.position + new Vector3(0f, playerHeight / 2, 0f));
Debug.Log(Input.GetAxis("Mouse Y"));
}
}
}
It's highly appreciated if you can help me.
I can not give you a proper answer to your question, but an advice: For moving objects or cameras, it is recommended to use FixedUpdate()
instead of Update()
(or LateUpdate()
). You can find tutorials about this topic on this site or even on youtube :)
Answer by tylerdtrudeau · Mar 13, 2021 at 04:05 PM
Ill admit I didnt look too hard at the code, but just quick question, Have you tried changing LateUpdate to FixedUpdate? if not can you try that and see if that helps.
Answer by CohenLocwood · Mar 15, 2021 at 12:15 AM
Instead of creating a script for the camera movement/controller, I used a unity package called cinemachine, which is faster and has high functionality.
Your answer
Follow this Question
Related Questions
Maximum Camera Angle Rotation 0 Answers
Camera Rotation around object. 0 Answers
How to rotate a camera around a spaceship freely 0 Answers
Top-Down Camera view (with rotation) need help (third person script) 0 Answers
Camera rotation 2 Answers