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 xpavelos · Sep 13, 2013 at 07:47 PM · timetimerinterrupt

Time - interrupting an action

Hi. I started playing with Unity and C# few weeks ago, and I am going deeper and deeper everyday... ;). Today I want to write an algorithm, but I have encountered a problem with time that I can't solve.

For sake of simplicity I have written points of what I want to achieve:

  1. There is an ACTION that requires for example 3 seconds to complete.

  2. If conditions are met (object is in ray, and space is pressed) - do ACTION (which takes 3 seconds)

  3. whenever ACTION is interrupted(at least one condition is not present) - stop ACTION

  4. whenever conditions are met again I want ACTION happen again and I want it to take again 3 seconds(do that action all over again)

I have almost done it, but the problem is:

I want to do something like timer: every time conditions of action are met - "Set timer to 3 seconds and start that timer.

Update() - in this function check if action has completed successfully and also whenever conditions are not met "turn timer off".

Thanks in advance.

UPDATE

I didn't want to make it complex, but I think you are right, I should post my code. But I have changed few things since I posted it, maybe I mixed something. So, the idea is: Player controls a ship and he can 'abduct' objects. That's the 'Action' I mentioned before. using UnityEngine; using System.Collections;

 public class Abduction : MonoBehaviour
 {
     GameObject player;
     Skills skills;
     GameObject ship;
     Transform shipTransform;
     Abductable abductable;
     bool isAbducting = false;
     float timeToFinish;
     
     void Start()
     {
         player = GameObject.Find ("Player");
         skills = player.GetComponent<Skills>();
         ship = GameObject.Find ("Ship");
         shipTransform = ship.transform;
 
     }
     
     void Update()
     {
         if(Input.GetKey (KeyCode.Space))
         {
             Ray ray = new Ray(shipTransform.position, shipTransform.up*-1);
             RaycastHit hit;
             Debug.DrawRay (ray.origin, ((shipTransform.up*-1)*skills.AbductionRange), Color.green);
             if (Physics.Raycast (ray, out hit, skills.AbductionRange))
             {
                 if (hit.collider.CompareTag("Abductable"))
                 {
                     abductable = hit.collider.GetComponent<Abductable>();
                     
                     SetTimeToFinish (abductable.Weight + skills.AbductionSpeed);
                     startAbduction(hit.collider, timeToFinish);
                     
                 }
                 
             }
             
         }
         
     }
     void startAbduction(Collider objectToAbduct, float timeToFinishAbduction)
     {
         isAbducting = true;
         Debug.Log ("Abducting... Time to finish: " + timeToFinishAbduction + " Total time: " + Time.time);
         Ray ray = new Ray (shipTransform.position, shipTransform.up*-1);
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit, skills.AbductionRange))
         {
             if (Time.time > timeToFinishAbduction)
             {
                 Debug.Log ("Abduction FINISHED");
                 Destroy (objectToAbduct.gameObject);    //destroy object
                 isAbducting = false;
             }// else isAbducting = false;
             
         }
     }
     
     void SetTimeToFinish(float finishTime)
     {
         if (!isAbducting)
         {
             
             timeToFinish = finishTime + Time.time;
         } //else timeToFinish = 0f;
     }
     
     
     
 }
 
Comment
Add comment · Show 2
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 Seizure · Sep 13, 2013 at 08:04 PM 0
Share

You would probably be better suited posting what you have for code. The only real way to answer your question right now is to just do it for you and most people won't do that.

avatar image Benproductions1 · Sep 14, 2013 at 10:15 AM 0
Share

Please post further information in the question, not as a comment

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by xpavelos · Sep 14, 2013 at 05:05 PM

Hey, I've found a solution. It works perfectly. I've just learned Static things in C# and I've created my own timer, that I can reset, stop or start any time I want. So if anyone is having similar problem to mine, here is working source code. So its designed for "2.5D" platformer, the effect is that player controls a ship and whenever spacebar is pressed he casts a ray that goes down (based on ship rotation). If anything is in ray that is 'abductable' ship will start abducting it. Abduction finishes in x seconds(based on player skill and weight of target). If target or ray didn't change and all other conditions are met (its in range and spacebar is down) target becomes abducted (you decide what to do).

(just to note, that this is just experimental code, so there are points where I should use static variables/methods, or catch types, but I'm just playing around, so its not a serious code)

So, here is code:

 using UnityEngine;
 using System.Collections;
 
 public class Abduction : MonoBehaviour
 {
     GameObject player;
     Skills skills;
     GameObject ship;
     Transform shipTransform;
     Abductable abductable;
     bool isAbducting = false;
     float timeToFinish;
     
     Collider cacheTarget;  //cache target, so when ship is abducting script can refer to this object
     Vector3 cacheOriginTarget;
     
     void Start()
     {
         player = GameObject.Find ("Player");
         skills = player.GetComponent<Skills>();
         ship = GameObject.Find ("Ship");
         shipTransform = ship.transform;
 
     }
     
     void Update()
     {
         if(Input.GetKey (KeyCode.Space))
         {
             Ray ray = new Ray(shipTransform.position, shipTransform.up*-1);
             RaycastHit hit;
             Debug.DrawRay (ray.origin, ((shipTransform.up*-1)*skills.AbductionRange), Color.green);
             if (Physics.Raycast (ray, out hit, skills.AbductionRange))
             {
                 if (hit.collider.CompareTag("Abductable"))
                 {
                     
                     abductable = hit.collider.GetComponent<Abductable>();
                     if (!isAbducting)
                     {
                         timeToFinish = skills.AbductionSpeed + abductable.Weight;
                         Debug.Log ("TimeToFinish: " + timeToFinish);
                         AbductionTimer.ResetTimer ();
                         AbductionTimer.StartTimer ();
                         isAbducting = true;
                         
                         //set start position
                         cacheOriginTarget = hit.collider.transform.position;
                         
                         cacheTarget = hit.collider;
                     }
                     if (isAbducting)
                     {
                         if (hit.collider == cacheTarget)    // check if target hasn't changed
                         {
                             
                             //HERE, 1.DECIDE WHAT TO DO WITH TARGET BEING ABDUCTED
                             //      2.DECIDE WHAT EVENTS SHOULD HAPPEN DURING 'ABDUCTION'
                             
                             
                             if(AbductionTimer.CurrentTime >= timeToFinish)
                             {
                                 //HERE DECIDE WHAT TO DO WHEN ABDUCTION IS FINISHED
                                 Debug.Log ("ABDUCTION FINISHED");
                                 Destroy (hit.collider.gameObject);
                                 isAbducting = false;
                             }
                         } else isAbducting = false;
 
                         
                     }
                 }else isAbducting = false;
 
             } 
                 
         } else isAbducting = false;
         
         if (!isAbducting)
         {
             AbductionTimer.StopTimer ();
             AbductionTimer.ResetTimer ();
             cacheTarget = null;
         }
             
     }
     
     void OnGUI()
     {
         GUI.Box (new Rect(10,65,150,25), "isAbducting: " + isAbducting);
         GUI.Box (new Rect(10,100,600,25), "cached target: " + cacheTarget);
         
     }
     
     
 }
 
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

17 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

Related Questions

Unity how to find load time? 1 Answer

creating a timed function 3 Answers

How to realize accurate time counter (millisecond precision) 3 Answers

How to check if no buttons are pressed for a specific time? 2 Answers

Timer Countdown fix. 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