using UnityEngine;
public class PlayerController : MonoBehaviour
{
public SpriteRenderer sr;
public Animator animator;
public Rigidbody2D rb;
public float horizontalMove = 0f;
public float runSpeed = 40f;
[Range(0, .3f)][SerializeField] private float movementSmoothing = .05f;
private Vector3 vector3 = Vector3.zero;
bool crouch = false;
// start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
realSpeed = runSpeed;
}
// update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
Jump();
CheckingGround();
SitingCheck();
if (rb.velocity.y < 0f && !onGround)
{
animator.SetBool("isFall", true);
}
if (rb.velocity.y == 0)
{
animator.SetBool("isFall", false);
}
}
void FixedUpdate()
{
Move(horizontalMove * Time.fixedDeltaTime);
}
public float jumpForce = 700f;
void Move(float move)
{
Vector3 targetVelocity = new Vector2(move * 10f, rb.velocity.y);
rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref vector3, movementSmoothing);
if (move > 0 && !faceRight)
{
Flip();
}
else if (move < 0 && faceRight)
{
Flip();
}
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.Space) && onGround && !jumpLock)
{
rb.AddForce(new Vector2(0f, jumpForce));
}
}
public bool faceRight = true;
private void Flip()
{
faceRight = !faceRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
public bool onGround;
public Transform GroundCheck;
public float checkRad = 0.3f;
public LayerMask Ground;
void CheckingGround()
{
onGround = Physics2D.OverlapCircle(GroundCheck.position, checkRad, Ground);
animator.SetBool("onGround", onGround);
}
public Transform TopCheck;
private float TopCheckRadius = 0.3f;
public LayerMask Roof;
public Collider2D poseStand;
public Collider2D poseSit;
private bool jumpLock = false;
public float realSpeed;
public float crouchSpeed = 20f;
void SitingCheck()
{
if (Input.GetKey(KeyCode.S))
{
animator.SetBool("isSit", true);
poseStand.enabled = false;
poseSit.enabled = true;
jumpLock = true;
runSpeed = crouchSpeed;
}
else if (!Physics2D.OverlapCircle(TopCheck.position, TopCheckRadius, Roof))
{
animator.SetBool("isSit", false);
poseStand.enabled = true;
poseSit.enabled = false;
jumpLock = false;
runSpeed = realSpeed;
}
}
}
if (rb.velocity.y < 0f && !onGround)
{
animator.SetBool("isFall", true);
}
if (rb.velocity.y == 0) // вот это вот условие
{
animator.SetBool("isFall", false);
}
animator.SetBool("isFall", !onGround);
void Jump() {
if (Input.GetKeyDown(KeyCode.Space) && onGround && !jumpLock) {
rb.velocity = new Vector2(rb.velocity.x, 0);
rb.AddForce(new Vector2(0f, jumpForce));
}
}
void Jump() {
if (Input.GetKeyDown(KeyCode.Space) && onGround && !jumpLock) {
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}