Wednesday, September 29, 2010

Damn, Smooth Sliding

Rewriting the code the sprite sheet animation from java script to C# has given me some problems I didn't anticipate. The code simply appears to work differently in C# than in java script. I have two videos that illustrate the difference.

This video shows the right animation but the code is in java script:


Here the similar code is rewritten in C# as you will see the sprite sheet just slides at an angle instead of jumping picture by picture:


Here's the original java script code:

//vars for the whole sheet
var colCount    : int =  4;
var rowCount    : int =  1;


//vars for animation
var rowNumber   : int =  0; //Zero Indexed
var colNumber   : int =  0; //Zero Indexed
var totalCells  : int =  4;
var fps  : int = 10;
var offset  : Vector2;  //Maybe this should be a private var


//Update
function Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);  }


//SetSpriteAnimation
function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int){


    // Calculate index
    var index : int = Time.time * fps;
    // Repeat when exhausting all cells
    index = index % totalCells;
    
    // Size of every cell
    var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
    
    // split into horizontal and vertical index
    var uIndex = index % colCount;
    var vIndex = index / colCount;

    // build offset
    // v coordinate is the bottom of the image in opengl so we need to invert.
    offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
    
    renderer.material.SetTextureOffset ("_MainTex", offset);
    renderer.material.SetTextureScale  ("_MainTex", size);
}


And the code in my C# version:

using UnityEngine;
using System.Collections;


public class Animation_sprite_sheet : MonoBehaviour {
//vars for the whole sheet
public int colCount =  4;
public int rowCount =  1;

//vars for animation
public int rowNumber = 0; //Zero Indexed
public int colNumber = 0; //Zero Indexed
public int totalCells = 4;
public int fps = 10;
Vector2 offset;  //Maybe this should be a private var


// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);
}
//SetSpriteAnimation
public void SetSpriteAnimation( int colCount, int rowCount, int rowNumber, int colNumber, int totalCells, int fps){
// Calculate index
     float index = Time.time * fps;
    
// Repeat when exhausting all cells
     index = index % totalCells;
    
   // Size of every cell
     Vector2 size = new Vector2 ((1.0f / colCount), (1.0f / rowCount));
    
     // split into horizontal and vertical index
   float uIndex = index % colCount;
     float vIndex = index / colCount;

     // build offset
     // v coordinate is the bottom of the image in opengl so we need to invert.
     Vector2 offset = new Vector2 ((uIndex+colNumber) * size.x, ((1.0f - size.y) - (vIndex+rowNumber) * size.y));
    
     renderer.material.SetTextureOffset ("_MainTex", offset);
     renderer.material.SetTextureScale  ("_MainTex", size);
}
}

As all the examples I can find in unity's documentation and around the internet on unity are mostly in java script I often find myself being lost when it comes to using C# in unity.

No comments:

Post a Comment