// Переменная для компонента Rigidbody2D
Rigidbody2D rb;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
// Получение компонента у носителя скрипта
// Носитель скрипта - объект, на котором накинут скрипт
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
// Проверка на ЗАЖАТИЕ клавиши
// isPressed - проверяет каждый кадр
if(Keyboard.current.dKey.isPressed)
{
rb.linearVelocityX = 5;
}
else if(Keyboard.current.aKey.isPressed)
{
rb.linearVelocityX = -5;
}
else
{
rb.linearVelocityX = 0;
}
// Проверка на прыжок
// wasPressedThisFrame - был нажат в текущем кадре
if(Keyboard.current.wKey.wasPressedThisFrame ||
Keyboard.current.spaceKey.wasPressedThisFrame)
{
rb.AddForce(new Vector2(0, 5), ForceMode2D.Impulse);
}
}