Hello everyone. When my object is pressed and moves along it, then there is such a twitch, as on a gif. Can you tell me how to fix it?
Вот мой код движения:
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(Rigidbody))]
public class MovementRigidbody : MonoBehaviour
{
[SerializeField] private float _moveSpeed;
[SerializeField] private float _rotationSpeed;
private Vector3 _targetDirection;
private float _inputAngle;
private float _rotationSmoothVelocity;
private float _lockAngleValue = 0.0f;
private Rigidbody _rigidbody;
private void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
Move();
}
public void OnMove(InputAction.CallbackContext context)
{
CreateTargetDirection(context.ReadValue<Vector2>());
}
private void Move()
{
if (_targetDirection.magnitude > 0.01f)
{
_rigidbody.velocity = _targetDirection * _moveSpeed;
Rotate();
}
}
private void CreateTargetDirection(Vector2 inputDirection)
{
_targetDirection = new Vector3(inputDirection.x, 0.0f, inputDirection.y).normalized;
}
private void Rotate()
{
_inputAngle = Mathf.Atan2(_targetDirection.x, _targetDirection.z) * Mathf.Rad2Deg;
float targetAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y,
_inputAngle, ref _rotationSmoothVelocity, _rotationSpeed);
_rigidbody.MoveRotation(Quaternion.Euler(_lockAngleValue, targetAngle, _lockAngleValue));
}
}