Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Do an action when I'm moved determined distance

Discussion in 'Scripting' started by zizau, Jun 6, 2016.

  1. zizau

    zizau

    Joined:
    Nov 10, 2014
    Posts:
    4
    Hello.
    I'm making a game and I want to do an action, for example print a text in the console, but only print the text one time, when I'm moved a determined distance. I made this script but does not work:
    (I use this script also for show the Walked distance in the screen.)
    Code (JavaScript):
    1. var WalkedDistance : float = 0;
    2. var lastPosition : Vector3;
    3. var style : GUIStyle;
    4.  
    5. function Start()
    6. {
    7. lastPosition = transform.position;
    8. }
    9.  
    10. function Update() {
    11. WalkedDistance += Vector3.Distance(transform.position, lastPosition);
    12.  
    13. lastPosition = transform.position;
    14.  
    15. if(WalkedDistance >= 100){   //This does not work
    16.     print("TEXT");
    17. }
    18. }
    19.  
    20. function OnGUI()
    21. {
    22.     GUILayout.Label("Walked distance = " + WalkedDistance);
    23. }
    24.  
    I hope someone can help me. Kind regards.
     
    Last edited: Jun 7, 2016
  2. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    The distance walked on any frame will never exactly equal 100.
    It will be off by some decimal amount.

    Try this:
    if(WalkedDistance > 100)
     
  3. zizau

    zizau

    Joined:
    Nov 10, 2014
    Posts:
    4
    I think in it, but if I use this script, then, the message will print infinite times and I want to print the message only one time. Would you know how to do it? c:
    Thanks for reply.
     
    Last edited: Jun 6, 2016
  4. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    you can create a flag boolean hasGone100
    set that to true inside the "if" statement
    and test for it too

    if(WalkedDistance > 100 && hasGone100 == false)
    hasGone100 = true;
     
    zizau likes this.
  5. zizau

    zizau

    Joined:
    Nov 10, 2014
    Posts:
    4
    I'm a bit noob in programming and Unity 3D and... how I create a flag boolean? :confused:
     
    Last edited: Jun 7, 2016
  6. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    There was no reply box in the message you sent me, not sure why.
    So I will answer here.

    If this is actually a project, then it sounds like school homework and you should do it yourself.
    If you dont know how to declare a boolean, or what one even is, you need to read a book, not ask questions on a forum.
    Thats the best advice I can give you.
     
  7. zizau

    zizau

    Joined:
    Nov 10, 2014
    Posts:
    4
    Ok. Indeed the project isn't serious, just I'm doing it to learn and have fun xD
    I searched how to a create a flag boolean and I wrote this script:
    Code (JavaScript):
    1. var WalkedDistance : float = 0;
    2. var lastPosition : Vector3;
    3. var style : GUIStyle;
    4. var hasGone100  : boolean = false;
    5.  
    6. function Start()
    7. {
    8. lastPosition = transform.position;
    9. }
    10. function Update() {
    11. WalkedDistance += Vector3.Distance(transform.position, lastPosition);
    12. lastPosition = transform.position;
    13. if(WalkedDistance > 10 && hasGone100 == false) {
    14.     hasGone100 = true;
    15.     print("TEXT");
    16. }
    17. }
    18. function OnGUI()
    19. {
    20.     GUILayout.Label("Walked distance = " + WalkedDistance);
    21. }
    Now works correct :) Thank you very much for your help ;)
     
  8. Tzan

    Tzan

    Joined:
    Apr 5, 2009
    Posts:
    733
    You're welcome.
    Congrats on figuring out the last part yourself :)