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);
}
}
}
[9417-screenshot+(5).png|9417]
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?
– ByteSheepYes 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
– FEARCOOKIELine 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 instead: 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 instead :)
– ByteSheepok i shall try it
– FEARCOOKIEyes!! 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?
– FEARCOOKIE