rabidus-test/Assets/Scripts/ShipMoveSides.cs

91 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipMoveSides : MonoBehaviour
{
public float MaxRollSpeed = 80;
public float MaxRollAngle = 45;
[SerializeField]
private float _radius;
[SerializeField]
[Range(-1,1)]
private float _value;
[SerializeField]
private bool _isEnable = true;
private ShipPathFollower _pathFollower;
private PlayerInputHandler _playerInput;
private void Awake()
{
_pathFollower = GetComponent<ShipPathFollower>();
}
private void Start()
{
_playerInput = GetComponent<PlayerInputHandler>();
}
public float Radius => _radius;
private void Update()
{
if (!_isEnable)
{
ResetInput();
return;
}
ApplyMovement();
}
public void ToggleInput(bool value)
{
_isEnable = value;
}
private void ResetInput()
{
_value = Mathf.Lerp(_value, 0, Time.deltaTime * 0.5f);
var newPos = transform.localPosition;
newPos.x = 0;
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;
}
public void UpdateInput(float val)
{
if (!_isEnable)
return;
_value = -val;
}
private void ApplyMovement()
{
if (_playerInput != null)
_value = -_playerInput.Horizontal;
var newPos = transform.localPosition + _value * Vector3.right;
newPos.x = Mathf.Clamp(newPos.x, -_radius, _radius);
transform.localPosition = Vector3.Lerp(transform.localPosition, newPos, Time.deltaTime * MaxRollSpeed * _pathFollower.SpeedPercent);
transform.localRotation = Quaternion.Euler(new Vector3(0, 0, _value * -MaxRollAngle * _pathFollower.SpeedPercent));
}
}