Вот сценарий движения:
Код: Выделить всё
public float jumpHeight;
public float speed;
public Rigidbody2D rb;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask Ground;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
void Update()
{
PausedThisFrame = false;
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, Ground);
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpHeight;
}
if (Input.GetKey(KeyCode.Space) && isJumping)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpHeight;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.A))
{
Debug.Log("A");
rb.velocity = new Vector2(-1 * speed, rb.velocity.y);
transform.eulerAngles = new Vector3(0, 180, 0);
}
if (Input.GetKey(KeyCode.D))
{
Debug.Log("D");
rb.velocity = new Vector2(1 * speed, rb.velocity.y);
transform.eulerAngles = new Vector3(0, 0, 0);
}
else
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
}
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... movement-k