Что в моем коде реализовано не правильно? Сломал себе уже всю голову. Кнопки все назначил, но при нажатии кнопки влево или вправо игрок просто стоит на месте.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
[SerializeField] private float speedForse = 7f;
[SerializeField] private float speed = 0f;
[SerializeField] private float jumpForse = 7f;
[SerializeField] private float wallJumpColdown;
[SerializeField] private LayerMask groundLayer;
[SerializeField] private LayerMask wallLayer;
[HideInInspector] public bool isFacingRight = true;
public bool RightButton = false;
public bool LeftButton = false;
public bool JumpButton = false;
private enum MovementState { idle, running, jumping, falling, walljumping, shoot }
private float dirX;
private Rigidbody2D body;
private BoxCollider2D boxCollider;
private Animator anim;
private PlayerShoot ps;
private void Start()
{
body = GetComponent<Rigidbody2D>();
boxCollider = GetComponent<BoxCollider2D>();
anim = GetComponent<Animator>();
ps = gameObject.GetComponent<PlayerShoot>();
}
private void Update()
{
//dirX = Input.GetAxisRaw("Horizontal");
//Проверка просмотра в какую сторону смотрит игрок для разварота текстуры
if (RightButton == true)
{
transform.localScale = Vector3.one;
print(body.velocity);
body.velocity = new Vector2(speedForse, body.velocity.y);
print(body.velocity);
isFacingRight = true;
}
else if (LeftButton == true)
{
transform.localScale = new Vector3(-1, 1, 1);
print(body.velocity);
body.velocity = new Vector2(- speedForse, body.velocity.y);
print(body.velocity);
isFacingRight = false;
}
//Прыжок от стены
if (Input.GetKey(KeyCode.Space) || JumpButton == true)
{
jump();
}
if (wallJumpColdown > 0.2f)
{
body.velocity = new Vector2(dirX * speedForse, body.velocity.y);
if (onWall() && !isGrounded())
{
body.gravityScale = 0;
body.velocity = Vector2.zero;
}
else
body.gravityScale = 1;
}
else
wallJumpColdown += Time.deltaTime;
UpdateAnimatState();
}
public void OnRightButton()
{
RightButton = true;
speed = speedForse;
}
public void OnLeftButton()
{
LeftButton = true;
speed = -speedForse;
}
public void OnJumpButton()
{
JumpButton = true;
}
private void jump()
{
if (isGrounded())
{
body.velocity = new Vector2(body.velocity.x, jumpForse);
}
else if (onWall() && !isGrounded())
{
if (dirX == 0)
{
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 10, 0);
transform.localScale = new Vector3(-Mathf.Sign(transform.localScale.x),
transform.localScale.y, transform.localScale.z);
}
else
{
body.velocity = new Vector2(-Mathf.Sign(transform.localScale.x) * 3, 6);
wallJumpColdown = 0;
}
}
}
private void UpdateAnimatState()
{
MovementState state;
if (dirX > 0f)
{
state = MovementState.running;
}
else
{
state = MovementState.idle;
}
if (body.velocity.y > 0.1f)
{
state = MovementState.jumping;
}
else if (body.velocity.y < -0.1f)
{
state = MovementState.falling;
}
if (body.gravityScale == 0f)
{
state = MovementState.walljumping;
}
if (ps.isShoot == true)
{
state = MovementState.shoot;
}
anim.SetInteger("state", (int)state);
}
private bool isGrounded()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center,
boxCollider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
return raycastHit.collider != null;
}
private bool isRoof()
{
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center,
boxCollider.bounds.size, 0, Vector2.up, 0.1f, groundLayer);
return raycastHit.collider != null;
}
}
private bool onWall()
{
RaycastHit2D raycastHit = Physics2D.BoxCast(boxCollider.bounds.center,
boxCollider.bounds.size, 0, new Vector2(transform.localScale.x, 0), 0.01f, wallLayer);
return raycastHit.collider != null;
}
public bool canAttack()
{
return dirX == 0 && isGrounded() && !onWall();
}
}