Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Square Monkey · Dec 21, 2013 at 04:41 PM · c#2djavascript

2D Sprite animation JS to C#

Tutorial 2D asset step 5

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

Javascript


C# foto. Not animation in place.

C#


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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiloblargh · Dec 21, 2013 at 04:55 PM 0
Share

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.

avatar image Square Monkey · Dec 24, 2013 at 05:36 PM 0
Share

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$$.

avatar image Square Monkey · Dec 24, 2013 at 05:37 PM 0
Share

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.

avatar image Seth-McCumber · Dec 24, 2013 at 06:39 PM 0
Share

Why not just use Unity 4.3?

avatar image Square Monkey · Dec 24, 2013 at 07:17 PM 0
Share

But i am using Unity 4.3? :O

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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..

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Square Monkey · Dec 24, 2013 at 07:30 PM 0
Share

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.

avatar image Square Monkey · Dec 24, 2013 at 08:18 PM 0
Share

btw i copied your second code and its full of errors

avatar image robertbu · Dec 24, 2013 at 08:20 PM 0
Share

You just need the 'new' keyword in front of Vector2 on lines 14 and 22.

avatar image Seth-McCumber · Dec 24, 2013 at 10:10 PM 0
Share

This may help

avatar image azmat786n · Dec 26, 2013 at 04:45 AM 0
Share

yup you need to change the line of vector to new vector(x,y) sorry for that, now it should work. :)

avatar image
0

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);
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges