- Home /
Check increasing float variable
I'm working on a script that changes the UV Offset to change the frames on a spritesheet. When I press the left mousebutton the next frame will be shown, but there are only 6 frames on the X so when the current frame reaches 6 it should go to the first Y frame which it does but only once. I know why but I don't really know how to fix it.
using UnityEngine;
using System.Collections;
public class animationScript : MonoBehaviour {
public float framesX = 6;
public float framesY = 6;
public float usedFrames = 0;
private float currentFrame = 0;
private float currentY = 0;
private float scrollY = 0;
private float scrollSpeed = 0;
private float scrollStop = 0;
public bool playAnim = true;
void Start () {
float startOffset = 0;
renderer.material.SetTextureOffset("_MainTex", new Vector2(startOffset, 0));
scrollSpeed = 1/framesX;
scrollY = -1/framesY;
scrollStop = scrollSpeed * usedFrames;
}
void Update () {
scrollY = scrollY;
float offset = scrollSpeed;
scrollSpeed = currentFrame/framesX;
if (Input.GetButtonDown("Fire1")){
currentFrame += 1;
}
if(currentFrame >= usedFrames){
currentFrame = 0;
currentY = 0;
}
if(playAnim == true){
scrollSpeed = scrollSpeed;
renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
}
if(currentFrame > framesX){
currentFrame = 0;
currentY = 1;
}
if(currentY == 1){
float offsetY = +scrollY;
renderer.material.SetTextureOffset("_MainTex", new Vector2(offset, offsetY));
}
}
}
currently it changes the Y offset when currentY == 1 but it needs to chance when currentY is increasing because the Y offset needs to change multiple times.
So what I need to know is: How do I check with an If statement if currentY is increasing ?
You should really, really consider using int
ins$$anonymous$$d of float
for your integers, such as currentY and framesX.
Answer by Graham-Dunnett · Jul 29, 2013 at 01:22 PM
This is how I'd do it. I think your variable called currentFrame
is actually the position in the X axis.
public float framesX = 6;
public float framesY = 6;
private float currentX = 0;
private float currentY = 0;
void Update () {
if (Input.GetButtonDown("Fire1")){
currentX += 1;
if (currentX == framesX) { // stepped off right side
currentX = 0;
currentY += 1;
if (currentY == framesY) { // stepped off top edge
currentY = 0;
}
}
}
// now do something with currentX and currentY
}
The only problem I have now is with:
if(currentY == 1 ){
float offsetY = +scrollY;
renderer.material.SetTextureOffset("_$$anonymous$$ainTex", new Vector2(offset, offsetY));
}
it only does it once, because "currentY == 1" but it needs to work for "currentY == 2" ect.. ect.. so when it increases it needs to use the SetTextureOffset.