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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2d texture animation + Player movment

Discussion in 'Scripting' started by lionbear, May 20, 2013.

  1. lionbear

    lionbear

    Joined:
    Mar 22, 2011
    Posts:
    12
    I am trying to make a 2.5D game with this script.

    Code (csharp):
    1. //vars for the whole sheet
    2. var colCount    : int =  4;
    3. var rowCount    : int =  4;
    4.  
    5. //vars for animation
    6. var rowNumber   : int =  0; //Zero Indexed
    7. var colNumber   : int =  0; //Zero Indexed
    8. var totalCells  : int =  4;
    9. var fps     : int = 10;
    10. var offset  : Vector2;  //Maybe this should be a private var
    11.  
    12. //Update
    13. function Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);  }
    14.  
    15. //SetSpriteAnimation
    16. function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int){
    17.  
    18.     // Calculate index
    19.     var index : int = Time.time * fps;
    20.     // Repeat when exhausting all cells
    21.     index = index % totalCells;
    22.  
    23.     // Size of every cell
    24.     var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
    25.  
    26.     // split into horizontal and vertical index
    27.     var uIndex = index % colCount;
    28.     var vIndex = index / colCount;
    29.  
    30.     // build offset
    31.     // v coordinate is the bottom of the image in opengl so we need to invert.
    32.     offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
    33.  
    34.     renderer.material.SetTextureOffset ("_MainTex", offset);
    35.     renderer.material.SetTextureScale  ("_MainTex", size);
    36. }
    i am trying to make a simple line of code that will stop my texture from animating! kinda like this!
    Code (csharp):
    1.  
    2.  
    3.         if(Input.anyKey == false)
    4.         {
    5.         animation.Stop();
    6.         }
    7.  
    8.  
    i am not sure how or where to impament this in the code? i need my character to stop animation. it should be simple.
     
  2. lionbear

    lionbear

    Joined:
    Mar 22, 2011
    Posts:
    12
    this is how my setup looks like. if your confused on what i am talking about! its going to be a top down rpg/scifi/shooter:D

    $MYROBOTSNAP.PNG
     
  3. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    As long as you call SetSpriteAnimation in update, your animation will run. What you can do is to have a boolean to trigger it on and off.
    Code (csharp):
    1.  
    2. if(Input.GetKey(KeyCode.W))
    3. {
    4.     canAnimate = true;
    5. }
    6. else
    7. {
    8.     canAnimate = false;
    9. }
    10.  
    Then in your update
    Code (csharp):
    1.  
    2. if(canAnimate)
    3. {
    4.     SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);
    5. }
    6.  
     
  4. lionbear

    lionbear

    Joined:
    Mar 22, 2011
    Posts:
    12
    thanks that works great!