Код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 1f;
public float jumpForce = 13f;
public Animator anim;
Rigidbody2D rb;
SpriteRenderer sr;
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
}
void Update()
{
float movement = Input.GetAxis("Horizontal");
transform.position += new Vector3(movement, 0, 0) * speed * Time.deltaTime;
if(Input.GetKeyDown(KeyCode.Space) && Mathf.Abs(rb.velocity.y) < 0.05f)
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
anim.SetFloat("movement", Mathf.Abs(movement));
if (Mathf.Abs(rb.velocity.y) < 0.05f)
{
anim.SetBool("jumping", false);
}
else
{
anim.SetBool("jumping", true);
}
sr.flipX = movement < 0 ? true : false;
}
}