using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroScript : MonoBehaviour
{
public float horizontalSpeed;
float speedX;
public float verticalimpulse;
public LayerMask groundLayer;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
bool IsGrounded()
{
Vector2 position = transform.position;
Vector2 direction = Vector2.down;
float distance = 1.0f;
Debug.DrawRay(position, direction, Color.black);
RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, groundLayer);
if (hit.collider != null)
{
return true;
}
return false;
}
void FixedUpdate()
{
if (Input.GetKey(KeyCode.A))
{
speedX = -horizontalSpeed;
transform.localScale = new Vector3(-1, 1); //Персонаж смотри в ту сторону, куда нажал игрок
}
else if (Input.GetKey(KeyCode.D))
{
speedX = horizontalSpeed;
transform.localScale = new Vector3(1, 1); //Персонаж смотри в ту сторону, куда нажал игрок
}
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
transform.Translate(speedX, 0, 0);
speedX = 0;
}
void Jump()
{
if (!IsGrounded())
{
return;
}
else
{
rb.AddForce(new Vector2(0, verticalimpulse), ForceMode2D.Impulse);
}
}
}