rabidus-test/Assets/Scripts/ShipMoveSides.cs

57 lines
1.2 KiB
C#
Raw Normal View History

2023-07-24 16:38:13 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipMoveSides : MonoBehaviour
{
[SerializeField]
private float _radius;
[SerializeField]
[Range(-1,1)]
private float _value;
[SerializeField]
private float _speed;
[SerializeField]
private float _rollAngle = 45;
2023-07-28 11:48:57 +03:00
[SerializeField]
private bool _isEnable = true;
2023-07-24 16:38:13 +03:00
public float Radius => _radius;
private void Update()
{
2023-07-28 11:48:57 +03:00
if (!_isEnable)
ResetInput();
2023-07-24 16:38:13 +03:00
ApplyMovement();
}
2023-07-28 11:48:57 +03:00
public void ToggleInput(bool value)
{
_isEnable = value;
}
private void ResetInput()
{
_value = Mathf.Lerp(_value, 0, Time.deltaTime);
}
2023-07-24 16:38:13 +03:00
public void UpdateInput(float val)
{
2023-07-28 11:48:57 +03:00
if (!_isEnable)
return;
2023-07-24 16:38:13 +03:00
_value = -val;
}
2023-07-28 11:48:57 +03:00
2023-07-24 16:38:13 +03:00
private void ApplyMovement()
{
var newPos = transform.localPosition + _value * Vector3.right;
newPos.x = Mathf.Clamp(newPos.x, -_radius, _radius);
transform.localPosition = Vector3.Lerp(transform.localPosition, newPos, Time.deltaTime * _speed);
transform.localRotation = Quaternion.Euler(new Vector3(0, 0, _value * -_rollAngle));
}
}