Files
RunRunRunP/Assets/Code/Player/Player.cs
T
2026-05-04 12:45:12 +03:00

49 lines
1.6 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
namespace Code.Player
{
public class Player : MonoBehaviour
{
[SerializeField] private Transform[] runPositions = new Transform[3];
[SerializeField] private float jumpStrength;
private Rigidbody _rigidbody;
private void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
InputSystem.actions.actionMaps[0].actions[0].performed += Jump;
InputSystem.actions.actionMaps[0].actions[1].performed += MoveRight;
InputSystem.actions.actionMaps[0].actions[2].performed += MoveLeft;
}
private void OnDestroy()
{
InputSystem.actions.actionMaps[0].actions[0].performed -= Jump;
InputSystem.actions.actionMaps[0].actions[1].performed -= MoveRight;
InputSystem.actions.actionMaps[0].actions[2].performed -= MoveLeft;
}
private void Jump(InputAction.CallbackContext context)
{
_rigidbody.AddForce(new(0,jumpStrength,0), ForceMode.Impulse);
}
private int _currentTarget = 1;
private void MoveRight(InputAction.CallbackContext context)
{
if(_currentTarget != 2) _currentTarget++;
ChangePosition();
}
private void MoveLeft(InputAction.CallbackContext context)
{
if(_currentTarget != 0) _currentTarget--;
ChangePosition();
}
private void ChangePosition()
{
transform.position = runPositions[_currentTarget].position;
}
}
}