• 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 FEARCOOKIE · Mar 28, 2013 at 01:06 AM · 2dcontroloffset

Script offset problem \(>.<)/

I'm making a 2d platformer where a player is controlled by the arrow keys , so far he can go left and right. I'm using a sprite sheet that I created resolution is 5337x796 its a simple walk-cycle 1 row and 8 columns. I'm using a plane to control the image.

THE PROBLEM:
the problem is that when the character moves the offset changes on the y axis, but since my image is 1 row ,well it looks weird. how can I alter my script to change the offset on the x axis instead of the y(like its doing currently).

Script:

 using UnityEngine;
 using System;
 using System.Collections;
  
 public class Test : MonoBehaviour
 {
     public int columns = 2;
     public int rows = 2;
     public float framesPerSecond = 10f;
         
     
     
     public float TranslationSpeed = 0.5f;
     public float RotationSpeed = 500f;
     float animationTime = 0f;
     int lastDirection = 1;
     
     //the current frame to display
     private float index = 0;
  
     
     
     void Update ()
     {
         // Get a value that indicates if the right/left keys are pressed:
         float horiz = Input.GetAxis("Horizontal");
         
         #region Translation
         // Move the character based on the right/left key press:
         float motion = horiz * Time.deltaTime * TranslationSpeed;
         transform.Translate(motion, 0, 0, Space.World);
         #endregion
         
         
         #region Cartoon animation
         animationTime += Math.Abs(horiz) * Time.deltaTime * framesPerSecond;
         if (animationTime >= 1f)
         {
             animationTime--;
             index++; 
             
             if (index >= rows * columns)
                 index = 0;
             
         }
         
         #endregion
         
         #region Flip figure
         // Figure out which direction the model should face based on the movement direction:
         int faceDirection = Math.Sign(horiz);
         if (faceDirection == 0) // If the model isn't moving, then keep it facing the previous way.
             faceDirection = lastDirection;
         lastDirection = faceDirection;
 
         // To give the impression of a piece of paper being flipped over, rotate the model to face the new direction over a period of time:
         float angle = transform.localEulerAngles.y;
         if (faceDirection == 1 && angle < 180f)
         {
             angle += Time.deltaTime * RotationSpeed;
             if (angle > 180f)
                 angle = 180f;
         }
         else if (faceDirection == -1 && angle > 0f)
         {
             angle -= Time.deltaTime * RotationSpeed;
             if (angle < 0f)
                 angle = 0f;
         }
         transform.localEulerAngles = new Vector3(0, angle, 0);
         #endregion
         
         
     }
         
         
         
     void Start()
     {
         StartCoroutine(updateTiling());
  
         //set the tile size of the texture (in UV units), based on the rows and columns
         Vector2 size = new Vector2(1f / columns, 1f / rows);
         renderer.sharedMaterial.SetTextureScale("_MainTex", size);        
     }
  
     private IEnumerator updateTiling()
     {
         while (true)
         {
           /*  
              //move to the next index
             index++;
             if (index >= rows * columns)
                 index = 0;
              */
         
  
             //split into x and y indexes
             Vector2 offset = new Vector2((float)index / columns - (index / columns), //x index
                                           (index / columns) / (float)rows);          //y index
  
             renderer.sharedMaterial.SetTextureOffset("_MainTex", offset);
  
             yield return new WaitForSeconds(1f / framesPerSecond);
         }
  
     }
 }

alt text

screenshot (5).png (89.0 kB)
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 ByteSheep · Mar 28, 2013 at 03:02 AM 0
Share

I'm guessing that you have already changed the rows and cols values in the inspector view?

 public int columns = 2;
 public int rows = 2;

These have to be set to the accurate rows and columns as I'm sure you are aware.
If you have got the right values assigned (rows=1, cols=8 in your case) then let me know and I will have another look over the code.. Btw, one thing i noticed is that the x value of the vector2 on line 100 in your code is always returning zero?
index / columns - (index / columns) will always be zero, won't it?

avatar image FEARCOOKIE · Mar 28, 2013 at 03:43 AM 0
Share

Yes i have set the values in the inspector ,and yea that's true thank you for pointing that out ,but still i don't know whats wrong with is i believe the error might be around line 35-45 Idk

avatar image ByteSheep · Mar 28, 2013 at 03:58 AM 0
Share

Line 35-45 control the speed at which the animation plays right? The code seems fine to me, couldn't spot anything wrong there.
But on line 100 the code will make the sprite offset on the y axis when the index variable changes and the x offset remains the same. Try this ins$$anonymous$$d:

 Vector2 offset = new Vector2(((float)index / columns) / (float)rows, 0);

This should make the sprite stay the same on the y offset and only animate on the x ins$$anonymous$$d :)

avatar image FEARCOOKIE · Mar 28, 2013 at 04:00 AM 0
Share

ok i shall try it

avatar image FEARCOOKIE · Mar 28, 2013 at 04:03 AM 0
Share

yes!! thank you so much it worked ,i just have one question ....if later on i have a sprite sheet with multiple rows how would i use this method?

Show more comments

1 Reply

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

Answer by ByteSheep · Mar 28, 2013 at 04:05 AM

I think this should work for the y offset too:

 Vector2 offset = new Vector2(((float)index / columns) / (float)rows, (index / columns) / rows);
  

Glad it worked ;)

Comment
Add comment · Show 10 · 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 FEARCOOKIE · Mar 28, 2013 at 04:14 AM 0
Share

ohh by the multiple rows I mean like move the offset as if reading a book ..left to right , then the next row and so on ..any ideas on that?

avatar image ByteSheep · Mar 28, 2013 at 04:18 AM 0
Share

oh yeah you are right! Sorry about that

avatar image FEARCOOKIE · Mar 28, 2013 at 04:22 AM 0
Share

but how can i make the code do that?

avatar image ByteSheep · Mar 28, 2013 at 04:23 AM 0
Share

I'll see what i can come up with, shouldn't be too hard to do

avatar image FEARCOOKIE · Mar 28, 2013 at 04:25 AM 0
Share

ok thank you so much ..its hard to find people that actually help you to this extent ty .

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

[2D] - Moving the child moves the parent <-- Unwanted behavior 1 Answer

Cannot offset the Z of a child camera? 1 Answer

Reversed Controls? 2 Answers

2D Ledge Climb, post climb animation doing wierd stuff (applying player offset). 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