Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float horizontalSpeed;
float speedX;
public float verticalImpulse;
Rigidbody2D rb;
bool isGrounded;
private bool facingRight = true;
void Start()
{
rb = GetComponent();
}
public void LeftButtonDown()
{
speedX = -horizontalSpeed;
}
public void RightButtonDown()
{
speedX = horizontalSpeed;
}
public void Stop()
{
speedX = 0;
}
public void OnClickJump()
{
if (isGrounded)
rb.AddForce(new Vector2(0, verticalImpulse), ForceMode2D.Impulse);
}
void FixedUpdate()
{
transform.Translate(speedX, 0, 0);
if (facingRight == false && horizontalSpeed > 0)
{
Flip();
}
else if (facingRight == true && horizontalSpeed < 0)
{
Flip();
}
}
void Flip()
{
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
isGrounded = false;
}
}