Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Script offset problem \(>.

$
0
0
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][1] [1]: /storage/temp/9417-screenshot+(5).png

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>