Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by fruitdealer · Nov 02, 2015 at 04:12 PM · timetransform.position

How can I compare transform.position now and transform.position after 2 seconds?

I'm making an endless runner and I want to restart the game if my player gets stuck for a short period of time.

Comment
Add comment · Show 1
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 fruitdealer · Nov 02, 2015 at 10:19 PM 0
Share

Thanks for the answers :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Fire_Cube · Nov 02, 2015 at 05:09 PM

you can set an int(eg : framesFor2Secs) with a certain amount of frame to get 2 seconds (2 / Time.deltaTime), and check if the position is the same as the posiion last frame. if it is the case, just add 1 to an int counter, if not, put it back to 0. If the conter reach framesFor2Secs, player was stuck for 2 seconds.

Comment
Add comment · 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
0

Answer by Statement · Nov 02, 2015 at 06:51 PM

I guess you'd check if the players position has moved from a known point. If player ventures beyond the deadzone from that point, timer is reset. If timer reaches an expiry, restart the game.

Here's a set of component that provides base functionality and two example implementations. One of them takes care of loading the level directly. The other allows you to listen for event and can be set up in the inspector.

Each of the namespaces should go into a file with the same name as the class.

 namespace ExampleImplementation
 {
     using Answers;
     using UnityEngine;
 
     [AddComponentMenu("Examples/Reload Level On Stuck")]
     public class ReloadLevelOnStuck : ExpiryDeadzone
     {
         protected override void OnExpire()
         {
             print("Timer has expired. Reloading level.");
             Application.LoadLevel(Application.loadedLevelName);
         }
     }
 }
 
 namespace ExampleImplementation
 {
     using Answers;
     using UnityEngine;
     using UnityEngine.Events;
 
     [AddComponentMenu("Examples/Event On Stuck")]
     public class EventOnStuck : ExpiryDeadzone
     {
         public UnityEvent onStuck;
         protected override void OnExpire()
         {
             onStuck.Invoke();
         }
     }
 }
 
 namespace Answers
 {
     using UnityEngine;
 
     public abstract class ExpiryDeadzone : MonoBehaviour
     {
         // Transform has to move 'deadzone.range' units to reset timer
         [SerializeField]
         private DeadZone deadzone;
 
         // Timer waits 'expiry' seconds before invoking OnExpire
         [SerializeField]
         private ExpiryTimer timer;
 
         // Allows OnExpire to be called multiple times
         public bool resetOnExpire = false;
 
         // Let user decide what happens on expiry.
         protected abstract void OnExpire();
 
         protected void Start()
         {
             ResetSystem();
         }
 
         protected void Update()
         {
             timer.Tick();
 
             if (deadzone.IsOutOfRange(transform.position))
                 ResetSystem();
 
             if (timer.isExpired)
             {
                 if (resetOnExpire)
                     ResetSystem();
                 else
                     enabled = false;
                 OnExpire();
             }
         }
 
         protected void ResetSystem()
         {
             enabled = true;
             timer.Reset();
             deadzone.pivot = transform.position;
         }
     }
 }
 
 namespace Answers
 {
     using System;
     using UnityEngine;
 
     [Serializable]
     public class ExpiryTimer
     {
         public float expiry = 2;
         public float elapsed { get; private set; }
 
         public bool isExpired
         {
             get { return elapsed >= expiry; }
         }
 
         public void Tick()
         {
             elapsed += Time.deltaTime;
         }
 
         public void Reset()
         {
             elapsed = 0;
         }
     }
 }
 
 namespace Answers
 {
     using System;
     using UnityEngine;
 
     [Serializable]
     public class DeadZone
     {
         public float range;
         public Vector3 pivot;
 
         public bool IsOutOfRange(Vector3 positon)
         {
             return Vector3.Distance(pivot, positon) > range;
         }
     }
 }
Comment
Add comment · 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

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

33 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

Related Questions

Time delay enemy respawn 3 Answers

Fixed Timestep and Solver Iteration Count Relationship 1 Answer

What is the best way to change sprite image when making movement? 1 Answer

How to get prefab's creation time and date? 2 Answers

Displaying C# Int Value to GUI 2 Answers


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