https://i.stack.imgur.com/3423d.png
https://i.stack.imgur.com/3423d.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header("Speeds")]
public float WalkSpeed = 3;
public float JumpForce = 10;
private MoveState _moveState = MoveState.Idle;
private DirectionState _directionState = DirectionState.Right;
private Transform _transform;
private Rigidbody2D _rigidbody;
private Animator _animatorController;
private float _walkTime = 0, _walkCooldown = 0.2f;
private bool _isGrounded = false;
private void Start()
{
_transform = GetComponent<Transform>();
_rigidbody = GetComponent<Rigidbody2D>();
_animatorController = GetComponent<Animator>();
_directionState = transform.localScale.x > 0 ? DirectionState.Right : DirectionState.Left;
}
private void Update()
{
HandleInput();
CheckGrounded();
if (_isGrounded)
{
if (_moveState == MoveState.Walk)
{
UpdateWalk();
}
else if (_moveState == MoveState.Idle)
{
Idle();
}
}
if (_moveState == MoveState.Jump)
{
UpdateJump();
}
}
private void HandleInput()
{
float horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput > 0)
{
MoveRight();
}
else if (horizontalInput < 0)
{
MoveLeft();
}
else
{
_moveState = MoveState.Idle;
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
public void MoveRight()
{
if (_moveState != MoveState.Jump)
{
_moveState = MoveState.Walk;
if (_directionState == DirectionState.Left)
{
_transform.localScale = new Vector3(-_transform.localScale.x, _transform.localScale.y, _transform.localScale.z);
_directionState = DirectionState.Right;
}
_walkTime = _walkCooldown;
// Определите анимацию только при фактическом движении
_animatorController.Play("Walk");
}
}
public void MoveLeft()
{
if (_moveState != MoveState.Jump)
{
_moveState = MoveState.Walk;
if (_directionState == DirectionState.Right)
{
_transform.localScale = new Vector3(-_transform.localScale.x, _transform.localScale.y, _transform.localScale.z);
_directionState = DirectionState.Left;
}
_walkTime = _walkCooldown;
// Определите анимацию только при фактическом движении
_animatorController.Play("Walk");
}
}
private void UpdateWalk()
{
float horizontalMovement = Input.GetAxis("Horizontal");
_rigidbody.velocity = new Vector2(horizontalMovement * WalkSpeed, _rigidbody.velocity.y);
if (horizontalMovement != 0)
{
// Определите анимацию только при фактическом движении
_animatorController.Play("Walk");
}
else
{
Idle();
}
}
public void Jump()
{
if (_isGrounded)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, JumpForce);
_moveState = MoveState.Jump;
_animatorController.Play("Jump");
}
}
private void UpdateJump()
{
if (_rigidbody.velocity.y == 0)
{
Idle();
}
}
private void Idle()
{
_moveState = MoveState.Idle;
_animatorController.Play("Idle");
}
private void CheckGrounded()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, 0.1f);
_isGrounded = hit.collider != null && _rigidbody.velocity.y == 0;
}
private enum DirectionState
{
Right,
Left
}
private enum MoveState
{
Idle,
Walk,
Jump
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerKeyboardController : MonoBehaviour
{
private Player player;
private void Start()
{
player = player == null ? GetComponent<Player>() : player;
if (player == null)
{
Debug.LogError("Player not set to controller");
}
}
private void Update()
{
if (player != null)
{
if (Input.GetKey(KeyCode.D))
{
player.MoveRight();
}
if (Input.GetKey(KeyCode.A))
{
player.MoveLeft();
}
if (Input.GetKeyDown(KeyCode.Space))
{
player.Jump();
}
}
}
}