Camera is Sliding

Hey there, so i am working on a RTS game and ran in to the problem that when i move my camera and want to stop the camera dosent stops instantly it rather slides a while till it stops

heres my code:

using UnityEngine;
using UnityEngine.EventSystems;

public class CameraController : MonoBehaviour
{
	public static CameraController instance;

	[Header("General")]
	[SerializeField] Transform cameraTransform;
	public Transform followTransform;
	Vector3 newPosition;
	Vector3 dragStartPosition;
	Vector3 dragCurrentPosition;

	[Header("Optional Functionality")]
	[SerializeField] bool moveWithKeyboad;
	[SerializeField] bool moveWithEdgeScrolling;
	[SerializeField] bool moveWithMouseDrag;

	[Header("Keyboard Movement")]
	[SerializeField] float fastSpeed = 0.3f;
	[SerializeField] float normalSpeed = 0.1f;
	[SerializeField] float movementSensitivity = 1f; // Hardcoded Sensitivity
	float movementSpeed;

	{
		UP,
		DOWN,
		LEFT,
		RIGHT,
		DEFAULT
	}

	private void Start()
	{
		instance = this;

		newPosition = transform.position;

		movementSpeed = normalSpeed;
	}

	private void Update()
	{
		// Allow Camera to follow Target
		if (followTransform != null)
		{
			transform.position = followTransform.position;
		}
		// Let us control Camera
		else
		{
			HandleCameraMovement();
		}

		if (Input.GetKeyDown(KeyCode.Escape))
		{
			followTransform = null;
		}
	}

	void HandleCameraMovement()
	{
		// Mouse Drag
		if (moveWithMouseDrag)
		{
			HandleMouseDragInput();
		}

		// Keyboard Control
		if (moveWithKeyboad)
		{
			if (Input.GetKey(KeyCode.LeftShift))
			{
				movementSpeed = fastSpeed;
			}
			else
			{
				movementSpeed = normalSpeed;
			}

			if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
			{
				newPosition += (transform.forward * movementSpeed);
			}

			if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
			{
				newPosition += (transform.forward * -movementSpeed);
			}
	
			if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
			{
				newPosition += (transform.right * movementSpeed);
			}

			if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
			{
				newPosition += (transform.right * -movementSpeed);
			}

		}


		transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementSensitivity);

	}





	private void HandleMouseDragInput()
	{
		if (Input.GetMouseButtonDown(2) && EventSystem.current.IsPointerOverGameObject() == false)
		{
			Plane plane = new Plane(Vector3.up, Vector3.zero);
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

			float entry;

			if (plane.Raycast(ray, out entry))
			{
				dragStartPosition = ray.GetPoint(entry);
			}
		}
		if (Input.GetMouseButton(2) && EventSystem.current.IsPointerOverGameObject() == false)
		{
			Plane plane = new Plane(Vector3.up, Vector3.zero);
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

			float entry;

			if (plane.Raycast(ray, out entry))
			{
				dragCurrentPosition = ray.GetPoint(entry);

				newPosition = transform.position + dragStartPosition - dragCurrentPosition;
			}
		}
	}
}

It’s sliding because of this:

transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementSensitivity);

You keep adding to newPosition while holding down keys but you’re outpacing what your lerp can keep up with. If the player isn’t holding any of the relevant movement keys, set newPosition to the current position.

Okay thanks for the anserw, could you explain how to change it to the current position because im relativ new to coding and this script is from a tutorial

Without doing a full refactor of your code, I’d do something like the following

  1. Add a private variable like private bool _moving
  2. In this section make sure to set the variable to true
if (moveWithKeyboad)
		{
			if (Input.GetKey(KeyCode.LeftShift))
			{
				movementSpeed = fastSpeed;
			}
			else
			{
				movementSpeed = normalSpeed;
			}

			if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
			{
				newPosition += (transform.forward * movementSpeed);
				_moving = true;
			}

			if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
			{
				newPosition += (transform.forward * -movementSpeed);
				_moving = true;
			}
	
			if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
			{
				newPosition += (transform.right * movementSpeed);
				_moving = true;
			}

			if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
			{
				newPosition += (transform.right * -movementSpeed);
				_moving = true;
			}
		}
  1. Below that, do the following:
if (_moving == true)
{
	transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementSensitivity);
}
else
{
	newPosition = transform.position;
}

_moving = false;

This way, the lerp will only happen if _moving == true and will set newPosition to the current position if not.

Now to address the elephant in the room: I don’t think you should be using lerp for the keyboard input at all. You can probably just modify transform.position directly.

it worked! thank you a lot and also thank you for the extra hint im going to learn something more about it and maybe im then able to change even that :slight_smile: