Hi everyone,
I’ve searched quite a bit to find a solution to the problem that I have.
Now the problem is, as I want to rotate the camerathere isn’t a problem.
But the problem occurs when I move the mouse to fast. Then my camera makes a weird movement.
Is there someone that has a solution to this? You’re most welcome.
Here is the code that I use on my camera:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera_Controller_1 : MonoBehaviour
{
public static Camera_Controller_1 instance;
public Transform cameratransform;
//KEYS
public KeyCode moveForward = KeyCode.Z;
public KeyCode moveBackward = KeyCode.S;
public KeyCode moveLeft = KeyCode.Q;
public KeyCode moveRight = KeyCode.D;
public KeyCode rotateLeft = KeyCode.A;
public KeyCode rotateRight = KeyCode.E;
//ALT KEYS
public KeyCode moveForwardALT = KeyCode.UpArrow;
public KeyCode moveBackwardALT = KeyCode.DownArrow;
public KeyCode moveLeftALT = KeyCode.LeftArrow;
public KeyCode moveRightALT = KeyCode.RightArrow;
//FLOATS
public float normalSpeed;
public float fastSpeed;
public float movementSpeed;
public float movementTime;
public float rotationAmount;
Vector3 rotateStartPosition;
Vector3 rotateCurrentPosition;
Quaternion newRotation;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
newPosition = transform.position;
newRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
HandleMovementInput();
HandleMouseInput();
}
void HandleMouseInput()
{
if (Input.GetMouseButtonDown(2))
rotateStartPosition = Input.mousePosition;
if (Input.GetMouseButton(2))
{
rotateCurrentPosition = Input.mousePosition;
Vector3 difference = rotateStartPosition - rotateCurrentPosition;
rotateStartPosition = rotateCurrentPosition;
newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / 5f));
}
}
void HandleMovementInput()
{
if (Input.GetKey(KeyCode.LeftShift))
movementSpeed = fastSpeed;
else
movementSpeed = normalSpeed;
if (Input.GetKey(moveForward) || Input.GetKey(moveForwardALT))
newPosition += (transform.forward * movementSpeed);
if (Input.GetKey(moveBackward) || Input.GetKey(moveBackwardALT))
newPosition += (transform.forward * -movementSpeed);
if (Input.GetKey(moveLeft) || Input.GetKey(moveLeftALT))
newPosition += (transform.right * -movementSpeed);
if (Input.GetKey(moveRight) || Input.GetKey(moveRightALT))
newPosition += (transform.right * movementSpeed);
if (Input.GetKey(rotateLeft))
newRotation *= Quaternion.Euler(Vector3.up * rotationAmount);
if (Input.GetKey(rotateRight))
newRotation *= Quaternion.Euler(Vector3.up * -rotationAmount);
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime);
}
}