Search Unity

Autostart an action after idling for a period of time

Discussion in 'Scripting' started by maurus, Feb 19, 2015.

  1. maurus

    maurus

    Joined:
    Jul 9, 2014
    Posts:
    11
    Hello guys

    I'd like to have a function that an action starts after the player is idling for a certain time.
    For example: I have started the application. Now I make a few clicks (or not) and then leave the computer. Then, after time=x (lets say 120 Seconds) I want to change the camera from the 1st person view to an other camera wich has an animated path through the level. As soon as I press "Esc" or so, I want to get back to the camera where it was after starting the application.
    Does make this sense to you?

    I've googled for this for about 3-4 hours now but still have no clue how to realize that. If someone could give me a hint how to do that, I'd be very thankful :)

    I'm fairly new to Unity (and coding), so please be nice to me :)

    Thank you and BR
    Maurus
     
  2. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Something ulgy (because I don't know how you handle your characters movements) :

    Code (CSharp):
    1.         public Vector3 lastPosition = Vector3.zero;
    2.  
    3.      
    4. void Update() {
    5.             if (lastPosition != Vector3.zero) {
    6.                 if (lastPosition == transform.position) {
    7.                     if (!IsInvoking("ChangeCameraPosition")) {
    8.                         Invoke("ChangeCameraPosition", 120);
    9.                     }
    10.                 } else {
    11.                     CancelInvoke("ChangeCameraPosition");
    12.                 }
    13.             }
    14.  
    15.             lastPosition = transform.position;
    16.         }
    17.  
    Edit: I don't know if it is "bad" to call several time per frame "CancelInvoke" but you could have a float "threshold" (when the positions are equal, you add Time.deltaTime to this variable, when it is not, you reset the variable, and if this variable is >= 3 seconds, you call "Invoke").
     
  3. maurus

    maurus

    Joined:
    Jul 9, 2014
    Posts:
    11
    Hi Sbizz
    Thank you for your answer! I have some difficulties to understand what's happening in your script. When I set the time from 120 down to 5 sec and place it on my player-object (I use the Standard 1st-person character controller from unity) and move around in the level, I can see that the "last position" coordinates are changing to my current position. And after 5 seconds not touching the mouse and/or KB - nothing happens.
    Shouldn't the player jump back to lastPosition as you defined it on line 1? (sorry, I only can do some HTML/css and a bit PHP codes, but no C# at all).
    Thank you brah!
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Did you create a function called "ChangeCameraPosition" in your class ? (or did you set the real function which switch cameras ?)

    Because it works for me

    Code (CSharp):
    1.     public Vector3 lastPosition = Vector3.zero;
    2.     public bool cameraChanged = false;
    3.  
    4.     public void ChangeCameraPosition() {
    5.         cameraChanged = true;
    6.  
    7.         Debug.Log("changeCameraPosition");
    8.     }
    9.  
    10.     void Update() {
    11.         if (lastPosition != Vector3.zero) {
    12.             if (lastPosition == transform.position) {
    13.                 if (!cameraChanged && !IsInvoking("ChangeCameraPosition")) {
    14.                     Debug.Log("invoke");
    15.                     Invoke("ChangeCameraPosition", 5);
    16.                 }
    17.             } else {
    18.                 if (IsInvoking("ChangeCameraPosition")) {
    19.                     Debug.Log("cancel");
    20.                     CancelInvoke("ChangeCameraPosition");
    21.                 }
    22.  
    23.                 cameraChanged = false;
    24.             }
    25.         }
    26.  
    27.         lastPosition = transform.position;
    28.     }
     
    maurus likes this.
  5. maurus

    maurus

    Joined:
    Jul 9, 2014
    Posts:
    11
    Hi Sbizz
    I think my coding "skills" end way too early to understand what you mean :D Because I still get this compile error: Expected class, delegate, enum, interface, or struct.
    Maybe it's better if I engage a freelance coder for this project (because there are more functions in my project I need to have and of wich I only have a slight idea how to make it).
    Thank you for your help!
     
  6. honprovet

    honprovet

    Joined:
    Mar 4, 2014
    Posts:
    23
    Lools like u didnt add code to actually change the camera position.

    The invoke only calls the method, it wont magically change the camera position. There must be code to make it happen!

    The second code posted above has a debug, so after 5 seconds you should read that debug. If you do, it means that the code worked, now all you must do is add the code to move the camera.

    Something like this might work
    Camera.main.transform.position += Vector3(0f, 0f, 1f);