public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float _speed;
[SerializeField] private float _jumpForce;
[SerializeField] private LayerMask _platformLayerMask;
private Rigidbody2D _rigidbody2d;
private BoxCollider2D _boxCollider2D;
private void Start()
{
_rigidbody2d = GetComponent<Rigidbody2D>();
_boxCollider2D = GetComponent<BoxCollider2D>();
}
private void Update()
{
Move();
Jump();
}
private void Move()
{
float horizontalInput = Input.GetAxis("Horizontal");
_rigidbody2d.velocity = new Vector2(horizontalInput * _speed, _rigidbody2d.velocity.y);
}
private void Jump()
{
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
_rigidbody2d.velocity = Vector2.up * _jumpForce;
}
}
private bool IsGrounded()
{
float extraHeightText = 0.1f;
RaycastHit2D raycastHit = Physics2D.Raycast(_boxCollider2D.bounds.center, Vector2.down, _boxCollider2D.bounds.extents.y + extraHeightText, _platformLayerMask);
Color rayColor;
if (raycastHit.collider != null)
rayColor = Color.green;
else
rayColor = Color.red;
Debug.DrawRay(_boxCollider2D.bounds.center, Vector2.down * (_boxCollider2D.bounds.extents.y + extraHeightText), rayColor);
return raycastHit.collider != null;
}
}
private IEnumerator TakeDamage(Transform directionToFly)
{
_spriteRenderer.color = Color.red;
float xDir = Mathf.Sign(transform.position.x - directionToFly.position.x);
Vector2 dir = new Vector2(xDir, 1);
_rigidbody2D.AddForce(dir * flyForce);
yield return new WaitForSeconds(0.3f);
_spriteRenderer.color = Color.white;
}
Оно сканить будет 5 часов так