{
//Walk
public float Speed = 7;
public Vector2 movement;
//Flip
public bool Right = true;
//Jump, Dash
public bool onGround;
//Jump
public float JumpForce = 60;
public float JumpControlTime = 0.3f;
private bool JumpControl;
private float JumpTime = 0f;
//SecondJump
public float DoubleJumpForce = 15;
public int JumpCount = 2;
private int MomentJumpCount = 0;
//Dash
public int DashImpulse = 5000;
//functions
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Jump();
}
void Update()
{
Walk();
SecondJump();
Dash();
}
void Walk()
{
movement.x = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(movement.x * Speed, rb.velocity.y);
if (movement.x < 0 && Right == true)
{
Flip();
Debug.Log("в лево");
}
else if (movement.x > 0 && Right == false)
{
Flip();
Debug.Log("в право");
}
}
public void Flip()
{
if ((onGround) && !JumpControl)
{
rb.velocity = new Vector2(rb.velocity.x, 0);
}
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
Right = !Right;
}
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;
}
}
void SecondJump()
{
if (Input.GetKeyDown(KeyCode.Space) && !onGround && !JumpControl && (++MomentJumpCount < JumpCount))
{
rb.velocity = new Vector2(0, DoubleJumpForce);
//Debug.Log(MomentJumpCount + "-Доп.прыжок");
}
}
void Dash()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
rb.velocity = new Vector2(rb.velocity.x, 0);
if (!Right)
{
rb.AddForce(Vector2.left * DashImpulse);
}
else
{
rb.AddForce(Vector2.right * DashImpulse);
}
//Debug.Log("Рывок!");
}
}
}
void Flip()
{
if ((onGround) && !JumpControl)
{
Vector2 normal = GetSurfaceNormal();
rb.velocity = Vector2.Reflect(rb.velocity, normal);
}
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
Right = !Right;
}
Vector2 GetSurfaceNormal()
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);
return hit.normal;
}
void FixedUpdate()
{
BugFix();
}
void Update()
{
Walk();
BugFixFPS();
}
void BugFixFPS()
{
if ((onGround) && !JumpControl)
{
CleanVectorY = true;
}
else
{
CleanVectorY = false;
}
}
void BugFix()
{
if ((onGround) && !JumpControl)
{
CleanVectorY = true;
}
else
{
CleanVectorY = false;
}
}
void Walk()
{
movement.x = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(movement.x * Speed, rb.velocity.y);
if (movement.x < 0 && Right == true)
{
Flip();
Debug.Log("в лево");
}
else if (movement.x > 0 && Right == false)
{
Flip();
Debug.Log("в право");
}
}
public void Flip()
{
if (CleanVectorY)
{
rb.velocity = new Vector2(rb.velocity.x, 0);
Debug.Log("Очистить вектор");
}
Vector3 scale = transform.localScale;
scale.x *= -1;
transform.localScale = scale;
Right = !Right;
}