Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MauritzLarsson · Nov 09, 2021 at 09:47 AM · inputtimerendless runnertimer countdowninfinite runner

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MauritzLarsson · Nov 09, 2021 at 10:29 AM 0
Share

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);
     }
 }
 






avatar image jmfp92 MauritzLarsson · Nov 09, 2021 at 10:47 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

164 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges