@vitek22

Как наложить функцию на кнопку клавиатуры в unity?

я посмотрел в ютубе туториалы расскаживающие на счет того как наложить на кнопку клавиатуры функцию, использовав, то что увидел и написал этот код:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class lvl_control2 : MonoBehaviour
{
[SerializeField] GameObject tablichka2lvl;
[SerializeField] GameObject podtext12lvl;
[SerializeField] GameObject podtext22lvl;
[SerializeField] GameObject podtext32lvl;
[SerializeField] GameObject podtext42lvl;
void Start()
{
StartCoroutine(Podtextt2lvl());
}
void Update()
{
bool propusk = Input.GetKeyUp(KeyCode.Space);
if(propusk)
{
StopCoroutine(Podtextt2lvl());
podtext12lvl.SetActive(false);
podtext22lvl.SetActive(false);
podtext32lvl.SetActive(false);
podtext42lvl.SetActive(false);
tablichka2lvl.SetActive(false);
}
}

IEnumerator Podtextt2lvl()
{
tablichka2lvl.SetActive(true);
podtext12lvl.SetActive(true);
podtext22lvl.SetActive(true);
podtext32lvl.SetActive(false);
podtext42lvl.SetActive(false);
yield return new WaitForSeconds(8f);
podtext12lvl.SetActive(false);
podtext22lvl.SetActive(false);
podtext32lvl.SetActive(true);
yield return new WaitForSeconds(8f);
podtext32lvl.SetActive(false);
podtext42lvl.SetActive(true);
}

все работает кроме функции пробела(Scape)
  • Вопрос задан
  • 185 просмотров
Пригласить эксперта
Ответы на вопрос 1
@MrStrong
Смотри у тебя где ты вызываешь StopCoroutine не срабатывает, потому что ты пытаешься остановить другую корутину, а не ту что ты запустил на старте.

Тебе надо на старте сохранить её в поле и потом её и отсанваливать. Вот так :
using System.Collections;
using UnityEngine;

public class lvl_control2 : MonoBehaviour
{
    [SerializeField] private GameObject tablichka2lvl;
    [SerializeField] private GameObject podtext12lvl;
    [SerializeField] private GameObject podtext22lvl;
    [SerializeField] private GameObject podtext32lvl;
    [SerializeField] private GameObject podtext42lvl;
    private Coroutine _podTextCoroutine;

    private void Start()
    {
        _podTextCoroutine = StartCoroutine(Podtextt2lvl());
    }

    private void Update()
    {
        bool propusk = Input.GetKeyUp(KeyCode.Space);

        if (propusk)
        {
            StopCoroutine(_podTextCoroutine);

            podtext12lvl.SetActive(false);
            podtext22lvl.SetActive(false);
            podtext32lvl.SetActive(false);
            podtext42lvl.SetActive(false);
            tablichka2lvl.SetActive(false);
        }
    }

    private IEnumerator Podtextt2lvl()
    {
        tablichka2lvl.SetActive(true);
        podtext12lvl.SetActive(true);
        podtext22lvl.SetActive(true);
        podtext32lvl.SetActive(false);
        podtext42lvl.SetActive(false);
        yield return new WaitForSeconds(8f);
        podtext12lvl.SetActive(false);
        podtext22lvl.SetActive(false);
        podtext32lvl.SetActive(true);
        yield return new WaitForSeconds(8f);
        podtext32lvl.SetActive(false);
        podtext42lvl.SetActive(true);
    }
}using System.Collections;
using UnityEngine;

public class lvl_control2 : MonoBehaviour
{
    [SerializeField] private GameObject tablichka2lvl;
    [SerializeField] private GameObject podtext12lvl;
    [SerializeField] private GameObject podtext22lvl;
    [SerializeField] private GameObject podtext32lvl;
    [SerializeField] private GameObject podtext42lvl;
    private Coroutine _podTextCoroutine;

    private void Start()
    {
        _podTextCoroutine = StartCoroutine(Podtextt2lvl());
    }

    private void Update()
    {
        bool propusk = Input.GetKeyUp(KeyCode.Space);

        if (propusk)
        {
            StopCoroutine(_podTextCoroutine);

            podtext12lvl.SetActive(false);
            podtext22lvl.SetActive(false);
            podtext32lvl.SetActive(false);
            podtext42lvl.SetActive(false);
            tablichka2lvl.SetActive(false);
        }
    }

    private IEnumerator Podtextt2lvl()
    {
        tablichka2lvl.SetActive(true);
        podtext12lvl.SetActive(true);
        podtext22lvl.SetActive(true);
        podtext32lvl.SetActive(false);
        podtext42lvl.SetActive(false);
        yield return new WaitForSeconds(8f);
        podtext12lvl.SetActive(false);
        podtext22lvl.SetActive(false);
        podtext32lvl.SetActive(true);
        yield return new WaitForSeconds(8f);
        podtext32lvl.SetActive(false);
        podtext42lvl.SetActive(true);
    }
}
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы