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 MC HALO · Mar 02, 2011 at 03:45 PM · spritespritesloopanimated

Sprite animate on start ?

hi Guys i created a script which makes my sprite move, what i did is i created an texture array which hold my sprite images i know i should use a sprite sheet but i dont like it that way, anyways what i want to do is when Unity runs i want the sprite to run automatically. I want my array to cycle through the number of elements within the array and once it has reached to the end i want it to restart. I have created a function at the bottom and that's where i want the code written in and then i want to call it on the start function here is my script:

the array that hold the images is var idleFrames : Texture [];

var MoveSpeed : float = 5;

var CurrentFrame: int =0;

var altTerryTexture : Texture[]; var TerryIdleTexture: Texture [];

var idleFrames : Texture [];

function Start (){

  StartSpriteAnime();

}

function Update () {

  if (Input.GetKey(KeyCode.A)){

     transform.Translate(Vector3(MoveSpeed * Time.deltaTime, 0 ,0));
     renderer.material.mainTexture = altTerryTexture[CurrentFrame];

  }

  if (Input.GetKey(KeyCode.D)){

     transform.Translate(Vector3(-MoveSpeed * Time.deltaTime , 0, 0));
     renderer.material.mainTexture = TerryIdleTexture[CurrentFrame]; 

  }

}

function StartSpriteAnime(){ }

thanks in advance :)

alt text

Comment
Add comment · Show 2
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 Peter G · Mar 02, 2011 at 09:45 PM 0
Share

We need to see more of your sprite system.

avatar image MC HALO · Mar 03, 2011 at 12:59 AM 0
Share

What do you mean ? do you want to see how the images are set and saved ? :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Alec-Slayden · Mar 03, 2011 at 04:08 AM

You might be better off just including it in the update rather than making it a coroutine, not sure. If you include a "var spriteDelay:int;" for the time( in seconds) between sprite frames, you could do something similar to this (Javascript examples!):

call it like so:

var repeatTime : float = spriteDelay *idleFrames.length;
InvokeRepeating("StartSpriteAnime",0, repeatTime);

That should properly loop the sprite animation.

function StartSpriteAnime(){

     for (tex in idleFrames){    

         renderer.material.mainTexture = tex;        
         yield.WaitForSeconds(spriteDelay);

     }
 }

Alternatively you could try using the mathf.repeat function:

function StartSpriteAnime(){

 while (playSpriteAnimation){    

     var timeOffset : float = Time.time / spriteDelay;
     var currentSprite : int = Mathf.Repeat(timeOffset, idleFrames.length-1);
     renderer.material.mainTexture = idleFrames[currentSprite];      
     yield;      
 }       

}

This second one involves a boolean "playSpriteAnimation" that you could set to false if needed to break the loop.

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 MC HALO · Mar 03, 2011 at 04:17 AM 0
Share

what is tex is that texture would i need to create a var for that

avatar image MC HALO · Mar 03, 2011 at 04:25 AM 0
Share

Hey dude your 1st one works but the problem now is it does not loop it plays once and that is it

avatar image Alec-Slayden · Mar 03, 2011 at 05:02 AM 0
Share

tex will be created automatically, you don't need to create a var for it. As for the 1st one working only once, looks like bad code, I don't think a function can call itself without errors, try the second one.

avatar image MC HALO · Mar 03, 2011 at 02:39 PM 0
Share

Where do i put the code:

var repeatTime : float = spriteDelay *idleFrames.length; InvokeRepeating("StartSpriteAnime",0, repeatTime);

under what line

sorry about this i am new to loops i hate them lol

avatar image MC HALO · Mar 03, 2011 at 02:54 PM 0
Share

Dude you are a life saver :) it works the second one :) you are a hero lol thank you so much :)

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

1 Person is following this question.

avatar image

Related Questions

Image is crushed --- Sprite 2 Answers

How to loop an array of 2d sprites 1 Answer

Explain this script :) plz 1 Answer

A node in a childnode? 1 Answer

An edge disappears from a sprite in GamePlay mode. 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