Hello devs,
here is a simpler solution to avoid snappy movement -
using System.Collections;
using System.Collections.Generic;
using System.Xml.Resolvers;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControls : MonoBehaviour
{
[SerializeField] InputAction movement;
[SerializeField] float controlSpeed = 20f;
[SerializeField] float xRange = 12f;
[SerializeField] float yRange = 10f;
[SerializeField] float positionPitchFactor = -2.0f;
[SerializeField] float controlPitchFactor = -20f;
[SerializeField] float positionYawFactor = 2.0f;
[SerializeField] float controlRollFactor = -20f;
[SerializeField] float lerpSpeed = 15f;
float xThrow, yThrow;
private void OnEnable()
{
movement.Enable();
}
private void OnDisable()
{
movement.Disable();
}
void Update()
{
ProcessTranslation();
ProcessRotation();
}
private void ProcessRotation()
{
float pitch = ProcessPitch();
float yaw = transform.localPosition.x * positionYawFactor;
float roll = xThrow * controlRollFactor;
// The part you are looking for
// Quaternion.Lerp(fromLocation, toLocation, speed) this should give you a rough idea how it works
Quaternion targetRotation = Quaternion.Euler(pitch, yaw, roll);
transform.localRotation = Quaternion.Lerp(transform.localRotation, targetRotation, Time.deltaTime * lerpSpeed);
}
private float ProcessPitch()
{
float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
float pitchDueToRotation = yThrow * controlPitchFactor;
float pitch = pitchDueToPosition + pitchDueToRotation;
return pitch;
}
private void ProcessTranslation()
{
float clampedXPos = ProcessXTranslation();
float clampedYPos = ProcessYTranslation();
transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
}
private float ProcessYTranslation()
{
yThrow = movement.ReadValue<Vector2>().y;
float yOffset = yThrow * controlSpeed * Time.deltaTime;
float rawYPos = transform.localPosition.y + yOffset;
float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);
return clampedYPos;
}
private float ProcessXTranslation()
{
xThrow = movement.ReadValue<Vector2>().x;
float xOffset = xThrow * controlSpeed * Time.deltaTime;
float rawXPos = transform.localPosition.x + xOffset;
float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
return clampedXPos;
}
}
PS - This is my first ever post here, suggestions are really appreciated, thank you:)