Search Unity

Looping problem

Discussion in 'Scripting' started by PaulHarisson, Feb 1, 2011.

  1. PaulHarisson

    PaulHarisson

    Joined:
    Jan 31, 2011
    Posts:
    13
    Go through this code.


    var Character : Transform;
    var dist = 3;

    function Update(){
    if ( Vector3.Distance( Character.position, transform.position ) < dist ){
    if (Input.GetKey("e"))
    Character.animation.Play("push");
    animation["push"].wrapMode = WrapMode.Once;
    }
    }





    I put this script on a game object say cube. The variable Character is my third person player. I have a third person character animation for pushing objects. ok. the thing is if i get near the cube and press "e" key, the player animation starts to play but it plays continuously. i want it to stop after one cycle of animation.

    it looks like pushing the object again and again lol.
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Is GetKeyDown() what you're looking for?
     
  3. PaulHarisson

    PaulHarisson

    Joined:
    Jan 31, 2011
    Posts:
    13
    The Problem is solved.:)



    var Character : Transform;
    var AnimationToPlay = "";
    var dist = 3;

    function Update(){
    if ( Vector3.Distance( Character.position, transform.position ) < dist ){
    if (Input.GetKey("e")){
    Character.animation[AnimationToPlay].wrapMode = WrapMode.ClampForever;
    Character.animation.Play(AnimationToPlay);


    }
    }
    }
     
  4. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    Brace brackets are your friend. So are tabs
     
  5. pavees

    pavees

    Joined:
    Jan 1, 2009
    Posts:
    116
    Code (csharp):
    1.  
    2. var Character : Transform;
    3. var AnimationToPlay = "";
    4. var dist = 3;
    5.  
    6. function Update(){
    7.       if ( Vector3.Distance( Character.position, transform.position ) < dist )
    8.       {
    9.             if (Input.GetKey("e"))
    10.             {
    11.                   Character.animation[AnimationToPlay].wrapMode = WrapMode.ClampForever;
    12.                   Character.animation.Play(AnimationToPlay);
    13.             }
    14.       }
    15. }
    16.  
    i guess now the tab is also a frnd... :)