{
//Walk
public float Speed = 7;
public bool Right = true;
private Vector2 movement;
//Jump
public bool onGround;
public float JumpForce = 60;
private bool JumpControl;
private float JumpTime = 0;
public float JumpControlTime = 0.3f;
public int JumpCount = 2;
public int MomentJumpCount = 0;
public float DoubleJumpForce = 20;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Walk();
Jump();
}
void Update()
{
SecondJump();
}
void Walk()
{
movement.x = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(movement.x * Speed, rb.velocity.y);
if (movement.x < 0 && Right == true)
{
Flip();
}
else if (movement.x > 0 && Right == false)
{
Flip();
}
}
public void Flip()
{
Right = !Right;
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
}
void Jump()
{
if (Input.GetKey(KeyCode.Space))
{
if (onGround)
{
JumpControl = true;
}
}
else
{
JumpControl = false;
}
if (JumpControl)
{
if ((JumpTime += Time.fixedDeltaTime) < JumpControlTime)
{
rb.AddForce(Vector2.up * JumpForce / (JumpTime * 10));
}
}
else
{
JumpTime = 0;
}
}
void SecondJump()
{
if (onGround)
{
MomentJumpCount = 0;
}
if (Input.GetKeyDown(KeyCode.Space) && !onGround && !JumpControl && (++MomentJumpCount < JumpCount))
{
rb.velocity = new Vector2(0, DoubleJumpForce);
}
}
}
void Jump()
{
if (Input.GetKey(KeyCode.Space))
{
if (onGround)
{
if (!JumpControl)
{
rb.velocity = new Vector2(rb.velocity.x, 0);
Debug.Log("Обновление вектора => Прыжок");
}
JumpControl = true;
}
}
else
{
JumpControl = false;
}
if (JumpControl)
{
if ((JumpTime += Time.fixedDeltaTime) < JumpControlTime)
{
rb.AddForce(Vector2.up * JumpForce / (JumpTime * 10));
}
}
else
{
JumpTime = 0;
}
if (onGround)
{
MomentJumpCount = 0;
}
}