rabidus-test/Assets/Scripts/ShipMoveSides.cs

91 lines
2.1 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
{
2023-10-02 19:12:35 +03:00
public float MaxRollSpeed = 80;
public float MaxRollAngle = 45;
2023-07-24 16:38:13 +03:00
[SerializeField]
private float _radius;
[SerializeField]
[Range(-1,1)]
private float _value;
2023-10-02 19:12:35 +03:00
2023-07-28 11:48:57 +03:00
[SerializeField]
private bool _isEnable = true;
2023-07-24 16:38:13 +03:00
2023-10-02 19:12:35 +03:00
private ShipPathFollower _pathFollower;
2023-10-18 11:59:26 +03:00
private PlayerInputHandler _playerInput;
2023-10-02 19:12:35 +03:00
private void Awake()
{
_pathFollower = GetComponent<ShipPathFollower>();
}
2023-10-18 11:59:26 +03:00
private void Start()
{
_playerInput = GetComponent<PlayerInputHandler>();
}
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)
2023-09-11 15:44:17 +03:00
{
2023-07-28 11:48:57 +03:00
ResetInput();
2023-09-11 15:44:17 +03:00
return;
}
2023-07-28 11:48:57 +03:00
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()
{
2023-10-11 15:44:05 +03:00
_value = Mathf.Lerp(_value, 0, Time.deltaTime * 0.5f);
2023-09-11 15:44:17 +03:00
var newPos = transform.localPosition;
newPos.x = 0;
2023-10-02 19:12:35 +03:00
transform.localPosition = Vector3.Lerp(transform.localPosition, newPos, Time.deltaTime * MaxRollSpeed * _pathFollower.SpeedPercent);
transform.localRotation = Quaternion.Euler(new Vector3(0, 0, _value * -MaxRollAngle * _pathFollower.SpeedPercent));
}
public void ChangeRollSpeed(float val)
{
MaxRollSpeed = val;
}
public void ChangeRollAngle(float val)
{
MaxRollAngle = val;
2023-07-28 11:48:57 +03:00
}
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()
{
2023-10-18 11:59:26 +03:00
if (_playerInput != null)
_value = -_playerInput.Horizontal;
2023-07-24 16:38:13 +03:00
var newPos = transform.localPosition + _value * Vector3.right;
newPos.x = Mathf.Clamp(newPos.x, -_radius, _radius);
2023-10-02 19:12:35 +03:00
transform.localPosition = Vector3.Lerp(transform.localPosition, newPos, Time.deltaTime * MaxRollSpeed * _pathFollower.SpeedPercent);
transform.localRotation = Quaternion.Euler(new Vector3(0, 0, _value * -MaxRollAngle * _pathFollower.SpeedPercent));
2023-07-24 16:38:13 +03:00
}
}