- Home /
How can i make the game check if a certain input is executed in a certain time frame?
Hi I'm a total beginner at unity, C# and programming in general. Im programming an endless runner for a school project. Now I want the player to press the "J" key on the keyboard while a certain asset travels past him before its too late. Im trying to solve this problem by starting a countdown timer when the asset spawns and making the game check if the "j" key was pressed inside of the determined time frame. If the key is pressed in time i want another object to spawn. If it is not pressed i want the character to be destroyed.
Answer by jmfp92 · Nov 09, 2021 at 10:00 AM
It is going to be really hard to answer this without seeing code but one thing you can do instead of using a timer would be to use distance which in my opinion would be way easier and most likely more efficient than a timer. assuming the project is in 2d (please add as much data about your project as possible in future questions) you could do something like
float maxDistanceFromPlayer = 5f;
GameObject targetAsset;
void Update(){
//if the player jumps at the right time
if(Input.GetKeyDown(KeyCode.Space) && Vector2.Distance(transform.position, targetAsset.transform.position) < maxDistanceFromPlayer){
//instantiate new prefab most likely after destroying last one
}
//if player jumps at wrong time
if(Input.GetKeyDown(KeyCode.Space) &&
Vector2.Distance(transform.position, targetAsset.transform.position) >
maxDistanceFromPlayer){
//destroy player object
}
I'd like to add that destroying your player object is rarely a good idea. instead you can just disable maybe the spriterenderer and collider and bring up your "restart" menu and when the scene reloads the player will show up again. Destroying gameobjects you intend to use again is never a good idea.
Hey thank you for answering! I understand i will try to give more information! Basically we have created an endless runner in 2.5D where the player controls a character flying a jetpack delivering food while avoiding obstacles (kind of like flappy bird but with 3D objects). Every now and then a house spawns which the character travels past and whenever the house is on screen we want the player to press a key button to throw a burrito through the window of the house. Because its a game for children and we have a very interactive way of playing the game using different sensors and circuitboards we don't want to make it too hard for them by them having to press the button at an exact moment but rather give them about 8 seconds to throw the burrito before its too late.
Here is the code for the building to spawn:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnHouse : MonoBehaviour
{
public GameObject House;
public float timeBetweenSpawn;
private float spawnTime;
// Update is called once per frame
void Update()
{
if (Time.time > spawnTime)
{
Spawn();
spawnTime = Time.time + timeBetweenSpawn;
}
}
void Spawn()
{
Instantiate(House, transform.position + new Vector3 (10, -4, 6), transform.rotation);
}
}
absolutely hopefully I can help you find a solution. The problem with using floats as timers is that like most program$$anonymous$$g languages, C# does indeed have a maximum float number it can reach before things start really not working as designed, and in this design you are constantly increasing the spawn time to a higher number. Most likely players will succeed or fail before the number reaches its maximum, but I did feel morally obligated to mention this for you to know in the future. If you do need to use a float I would do something like this for a timer
bool houseOnScreen;
float onScreenTimer = 8f, ogOnScreenTimer;
void Start(){
ogOnScreenTimer = onScreenTimer;
}
void Update(){
if(houseOnScreen){
onScreenTimer -= Time.DeltaTime;
if(onScreenTimer <= 0f){
onScreenTimer = ogOnScreenTimer;
//reset level however you see fit
}
void Spawn(){
//the code you already have
houseOnScreen = true;
}
above you can see that you are adding even more floats that are going to have to be manipulated and while this will work, I still believe the distance formula will in the end be more efficient. Remember that computers really use frames as a cheat code for time ( that's why you have to use Time.DeltaTime to ensure each frame runs at the expected time) and while I'm not sure that it is a 1:1 if you were to use the distance option I suggested earlier and set the maxdistance to 8f, it would most likely be around 8 seconds the player would have to press the burrito button, so really whatever works for you and hopefully I helped.
Edit: running the scenario through my head, if you did elect to use distance you probably only want the x value from the vector2 that is returned from vector2.distance, considering the houses y value would be different from the players and could potentially effect the amount of time the player has to throw the burrito.
PS great game idea! who doesn't love throwing burritos?
Your answer
Follow this Question
Related Questions
Random obstacles spawn in 2D side endless runner game ? 2 Answers
Save Time Button Settings in Player Prefs 2 Answers
Timer question 1 Answer
Speed up timer 1 Answer
Start countdown timer with condition 1 Answer