Showing posts with label animation. Show all posts
Showing posts with label animation. Show all posts

Friday, October 22, 2010

Unity Crash Course

This week I attended a lecture series in Unity hosted by ITU. Two nice guys from Unity, Joe and Emil, gave a guide tour through a lot of Unity's features from animation, textures, GUI and editor scripting. They covered tons of stuff and it turned out to be a nice introduction to Unity.

Adding to the greatness of it all was the time given to questions from the audience. I got to ask what was wrong with the sprite sheet animation code since it was giving my this odd graphics problem over time. At first Joe found it just as strange as me but after a trip back to the Unity-factory he could give me the solution.

It turns out the offset float value on the y-axe kept in increase over time. This would be fine if it wasn't for the fact that the float as it increased would be round up thereby constantly increasing in inaccuracy.

Solution: Adding Mathf.Repeat to the offset's x value with the length of 1.
offset.y = Mathf.Repeat(offset.y,1);
This ensures the value every time it goes above 1 is returned to count from 0 instead.

And now it works! Yaaahhhhhh!

Sunday, October 17, 2010

Quickie


4 frame animation of someone walking.

Saturday, October 2, 2010

It Moves!

I've got the code for animating sprite sheets working. I had made the mistake of setting a variable as a float instead of an integer. In human lingo this mean I was working with a decimal number where I was supposed to use a "whole" number. I took me a while to figure this out, but by now at least I understand the code I borrowed a lot better.

I changed this:
float index = Time.time*fps;
to this:
int index = (int)(Time.time*fps);

Current screenshot of application:


Everything looks a mess. I'm trying to figure out why my graphics gets pixelated now that I've change to Unity3 and while doing so I've piled graphics in different resolutions.

Talking of Unity3 the mysterious icon issue has been fixed and it's now super easy to add an icon picture under the player settings and best of all - it works!

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.

Monday, September 6, 2010

float progress = 0;

No progress today. I guess I tried to study. I was looking for tutorials on animating with sprite sheets something that would give me a good work method for vector animation. I didn't find anything useful. Mostly forum posts about how to convert a 3d animation to sprite sheets and often with very limited info. If I find a good method by myself then I guess I should write it down.

I did find one post at angryanimator.com on animation in general which is nice. Not what I was looking for but still worth a look. Especially the links to “How to Animate” by Preston Blair volume 1 and volume 2 are great. One of the comments mentions the book Character Animation Crash Course! maybe I should find a copy.

Also, I looked a bit at the code but I guess I wasn't in the zone.