- Home /
2D Sprite animation JS to C#
This is the tutorial I am following. Now in this part we are letting the 2d sprite move by the code and if done correctly you will see an animated character. Lovely.
When I plant the code the tutorial guy is providing me with on the square with the mushroom (so im planting the javascript on it) it works like he is showing.
However if I use the C# on the square mushroom it doesn't work and it will just slide the entire texture from left to right or vice versa.
help?!?!
Javascript foto. Animation in place
C# foto. Not animation in place.
Javascript
var column : int;
var row : int;
var framesPerSecond = 8.0;
function Update ()
{
var index : int = Time.time * framesPerSecond;
index = index % (column * row);
var size = Vector2 (1.0 / column, 1.0 / row);
var offset = Vector2 (index * size.x, row);
renderer.material.mainTextureOffset = offset;
renderer.material.mainTextureScale = size;
}
C#
using UnityEngine;
using System.Collections;
public class Step5 : MonoBehaviour
{
public int column = 16;
public int row = 1;
public int framesPerSecond = 8;
void Update ()
{
float index = Time.fixedTime * framesPerSecond; //time control fps
index = index % (column * row); //modulate
Vector2 size = new Vector2 (1.0F / column, 1.0F / row); //scale
Vector2 offset = new Vector2 (index * size.x, row); //offset
renderer.material.mainTextureOffset = offset; //texture offset
renderer.material.mainTextureScale = size; //texture scale
}
}
I'm not getting any errors as the code works, just not the way it should happen. I tried changing the time to deltaTime, i tried changing values. Yet again, I am stuck.
It has nothing to do with C# vs JS, it's because you changed index
from an int in the first script to a float in the second.
yeah thats correct i did that. And the reason for that is when i put it in a integer monodevelop gave me errors. I'll try and post the code with int and error's in a couple $$anonymous$$.
got this code now
using UnityEngine;
using System.Collections;
public class Step5 : $$anonymous$$onoBehaviour
{
public int column = 16;
public int row = 1;
public int framesPerSecond = 8.0;
void Update ()
{
int index = Time.fixedTime * framesPerSecond; //time control fps
index = index % (column * row); //modulate
Vector2 size = new Vector2 (1.0F / column, 1.0F / row); //scale
Vector2 offset = new Vector2 (index * size.x, row); //offset
renderer.material.mainTextureOffset = offset; //texture offset
renderer.material.mainTextureScale = size; //texture scale
}
}
and it gives me this error
Assets/Scripts C#/Step5.cs(8,40): error CS0031: Constant value `8' cannot be converted to a `int'
and when i changed it to a float the code stopped giving me errors.
Answer by azmat786n · Dec 24, 2013 at 06:32 PM
here is two different ways to run animation
this is for image sequence
public Texture2D[] frames;
public float framesPerSecond = 10.0f;
void Update () {
float index = Time.time * framesPerSecond;
index = index % frames.Length;
renderer.material.mainTexture = frames[index];
}
Here is second method to run animation sheet
public float uvAnimationTileX = 6;
public float uvAnimationTileY = 1;
float framesPerSecond = 10.0f;
void Update () {
// Calculate index
float index = Time.time * framesPerSecond;
// repeat when exhausting all frames
index = index % (uvAnimationTileX * uvAnimationTileY);
// Size of every tile
var size = Vector2 (1.0f / uvAnimationTileX, 1.0f / uvAnimationTileY);
// split into horizontal and vertical index
var uIndex = index % uvAnimationTileX;
var vIndex = index / uvAnimationTileX;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
var offset = Vector2 (uIndex * size.x, 1.0f - size.y - vIndex * size.y);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
Hope this is helpful for you..
thank you very much but i am following this tutorial and the second method is what i probably learn in a more advanced video down the road. So i need to change it to a float.
btw i copied your second code and its full of errors
You just need the 'new' keyword in front of Vector2 on lines 14 and 22.
yup you need to change the line of vector to new vector(x,y) sorry for that, now it should work. :)
Answer by Alexander21 · Mar 04, 2017 at 08:43 AM
This is the Perfect working code`enter code here` using UnityEngine; using System.Collections;
public class AnimationSS : MonoBehaviour {
// Use this for initialization
public float uvAnimationTileX = 6;
public float uvAnimationTileY = 1;
float framesPerSecond = 10.0f;
void Update () {
// Calculate index
float index = Time.time * framesPerSecond;
// repeat when exhausting all frames
index = index % (uvAnimationTileX * uvAnimationTileY);
// Size of every tile
Vector2 size =new Vector2 (1.0f / uvAnimationTileX, 1.0f / uvAnimationTileY);
// split into horizontal and vertical index
float uIndex = index % uvAnimationTileX;
float vIndex = index / uvAnimationTileX;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
Vector2 offset = new Vector2 (uIndex * size.x, 1.0f - size.y - vIndex * size.y);
GetComponent<Renderer>().material.SetTextureOffset ("_MainTex", offset);
GetComponent<Renderer>().material.SetTextureScale ("_MainTex", size);
}
}
Your answer
Follow this Question
Related Questions
Destroy Object On Collision? 3 Answers
Script won't destroy prefab clones... 1 Answer
Multiple Cars not working 1 Answer
; expected. Insert a semicolon... C# & JavaScript 2D Controller 0 Answers
2D games; Javascript or C#? 1 Answer