Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Hero : MonoBehaviour { [SerializeFrield] private float speed = 3f; [SerializeFrield] private int lives = 5; [SerializeFrield] private float jumpForce = 15f; private bool isGrounded = false; private Rigidbody rd; private SpriteRenderer sprite; private void Awake() { rb = GetComponent(); sprite = GetComponentInChildren(); } private void FixedUpdate() { CheckGround(); } private void Update() { if (Input.GetButon("Horizontal")) Run(); if (isGrounded && Input.GetButonDown(Jump)) Jump(); } private void Run() { Vector3 dir = transform.right * Input.GetAxis("Horizintal"); transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime); sprite.flipx = dir.x < 0.0f; } private void Jump() { rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse); } private void CheckGround() { Collider2D[] collider = Physiscs2D.OverlapCircleAll(transform.position, 0.3f); isGrounded = collider.Length > 1; } }