Hi all, I have one button (or tap on screen) that executes a different action (e.g. shoot, pull lever, eat) depending on a necessary condition (has gun, has lever, has cake) otherwise executes a default action (jump).
Two actions shouldn’t happen at the same time and I want a delay between the actions
This is the file that translates the button/tap into either an action or a jump, and adds a delay. And isn’t working
public class PlayerActionManager : MonoBehaviour
{
private bool _isJumping;
public bool IsJumpig { get => _isJumping; }
private PlayerInput _playerInput;
private List<IActionable> _actionables = new List<IActionable>();
private float _timer;
private void Start()
{
var actionable = FindObjectsOfType<MonoBehaviour>().OfType<IActionable>();
foreach (IActionable a in actionable)
{
_actionables.Add(a);
}
_playerInput = GetComponent<PlayerInput>();
}
private void Update()
{
foreach (IActionable actionable in _actionables)
{
if (actionable.CanExecuteAction)
{
if (_playerInput.ActionInput)
{
actionable.ExecuteAction();
_timer = 1f;
}
}
else
{
if (_timer < 1f)
{
_timer += Time.deltaTime;
}
else
{
_isJumping = _playerInput.ActionInput;
_timer = 0;
}
}
}
}
}
Here’s the rest of the setup (simplified code)
public class PlayerInput : MonoBehaviour
{
public bool ActionInput
{
get
{
if (_touchInput != null)
{
if (_touchInput.screenTouched)
{
return true;
}
}
return Input.GetKey(KeyCode.UpArrow);
}
}
}
public class CharachterMovement : MonoBehaviour
{
void Update()
{
Move(_playerActionManager.IsJumping);
}
}
public void Move(bool canJump)
{
if (IsGrounded && canJump)
{
IsGrounded = false;
_rigidbody2D.AddForce(new Vector2(0f, _jumpForce));
}
}
}
public interface IActionable
{
bool CanExecuteAction { get; }
void ExecuteAction();
}
public class PickUpManager : MonoBehaviour, IActionable
{
private bool _isLoaded;
public bool CanExecuteAction { get => _isLoaded; }
private void OnTriggerEnter2D(Collider2D collision)
{
_isLoaded = true;
}
}
private void Update()
{
if (_canThrow)
{
_throwable.Throw();
_isLoaded = false;
_canThrow = false;
}
}
}
public void ExecuteAction()
{
_canThrow = true;
}
}