void FindAnimation() { // Find which animation is currently playing // one clue is if state is FightingPlayer. if it is set the current animation to animationFight if (state == ControlState.FightingPlayer || currentAnimation == AnimationLoops.animationFight) { currentAnimation = AnimationLoops.animationFight; return; } // if the inputMovement vector has a positive magnitude, then the sprite is walking if (inputMovement.magnitude > 0) { currentAnimation = AnimationLoops.animationWalk; } else { // if both these conditions fail, the sprite is standing still waving! currentAnimation = AnimationLoops.animationWave; } }
void FindAnimation() { // Find which animation is currently playing // one clue is if state is FightingDroid. if it is set the current animation to animationFight if (state == ControlState.FightingDroid || currentAnimation == AnimationLoops.animationFight) { currentAnimation = AnimationLoops.animationFight; return; } // if the inputMovement vector has a positive magnitude, then the sprite is walking if (state == ControlState.MovingDroid || inputMovement.magnitude > 0) { currentAnimation = AnimationLoops.animationWalk; } else { // if both these conditions fail, the sprite is standing still waving! currentAnimation = AnimationLoops.animationWave; } }
void ProcessAnimation() { // iteration int i int i; // the animation framerate is managed by setting animationTime initially to 1/framerate // then subtracting the gameplay framerate passed since the last call to ProcessAnimation // When animationTime eventually becomes negative after enough subtractions, it is time // to display a new frame of animation. Then, after the new animation frame is displayed, we // reset animationTime to 1/framerate, so we can begin the process again // This essentially creates a Flash-like local timeline for this sprite using the gameplay framerate animationTime -= Time.deltaTime; // if the game is running at 30 frames per second // animationTime will subtract 0.033 of a second (1/30) if (animationTime <= 0) { frameNumber += 1; // manage the animationFight animation if (currentAnimation == AnimationLoops.animationFight) { frameNumber = Mathf.Clamp(frameNumber, fightAnimationMin, fightAnimationMax + 1); spriteScalerY = 0.9150f; spriteFightY = -0.009f; if (frameNumber > fightAnimationMax) { // once we have finished the fight animation, detect if we have hit the player again doFightDamage = false; if (state == ControlState.FightingDroid) { // when looping the fight animation it looks better to start at fightAnimationMin + 4 frameNumber = fightAnimationMin + 4; } else { currentAnimation = AnimationLoops.animationWalk; frameNumber = walkAnimationMin; } } // if this droid is within 2 units of the player and if we are at least 4 frames into the animation, and we haven't attacked yet if (state == ControlState.FightingDroid && frameNumber > fightAnimationMin + 4 && doFightDamage == false) { // damage the player doFightDamage = true; if (gameObject != null && objPlayer != null) { playerScript ptrPlayerScript = (playerScript)objPlayer.GetComponent(typeof(playerScript)); ptrPlayerScript.maintenancePoints -= fightDamage; //print ("Attacked by " + gameObject.tag); } } } // manage the animationWave animation if (currentAnimation == AnimationLoops.animationWave) { frameNumber = Mathf.Clamp(frameNumber, waveAnimationMin, waveAnimationMax + 1); spriteScalerY = 0.8918f; spriteFightY = 0f; if (frameNumber > waveAnimationMax) { frameNumber = waveAnimationMin; } } // manage the animationWalk animation if (currentAnimation == AnimationLoops.animationWalk) { frameNumber = Mathf.Clamp(frameNumber, walkAnimationMin, walkAnimationMax + 1); spriteScalerY = 0.9150f; spriteFightY = 0f; if (frameNumber > walkAnimationMax) { frameNumber = walkAnimationMin; } } // reset the animationTime to 1/framerate, so we can begin the local timeline process again animationTime += (1 / animationFrameRate); } // The remaining code finds the image for the animation frame on the sprite sheet, // then displays it on the surface of the droid's plane model as a texture // First, find the number of frames down the animation is on the sprite sheet and set the y coordinate accordingly spriteSheetCount.y = 0; for (i = (int)frameNumber; i > (int)spriteSheetTotalRows; i -= (int)spriteSheetTotalRows) { spriteSheetCount.y += 1; } // Second, find the number of frames down the animation is on the sprite sheet and set the x coordinate accordingly spriteSheetCount.x = i - 1; // Third, calculate the exact X and Y pixel coordinate on the sprite sheet of the frame to display double calcY = spriteScalerY * (spriteSheetCount.y / spriteSheetTotalRows) + spriteFightY; double calcX = 0.9150 * (spriteSheetCount.x / spriteSheetTotalCols) + 0.033; spriteSheetOffset = new Vector2((float)(1 - calcX), (float)(1 - calcY)); // Last, offset the texture to display the correct frame renderer.material.SetTextureOffset("_MainTex", spriteSheetOffset); }
void ProcessAnimation() { // iteration int i int i; // the animation framerate is managed by setting animationTime initially to 1/framerate // then subtracting the gameplay framerate passed since the last call to ProcessAnimation // When animationTime eventually becomes negative after enough subtractions, it is time // to display a new frame of animation. Then, after the new animation frame is displayed, we // reset animationTime to 1/framerate, so we can begin the process again // This essentially creates a Flash-like local timeline for this sprite using the gameplay framerate animationTime -= Time.deltaTime; // if the game is running at 30 frames per second // animationTime will subtract 0.033 of a second (1/30) if (animationTime <= 0) { frameNumber += 1; // manage the animationFight animation if (currentAnimation == AnimationLoops.animationFight) { frameNumber = Mathf.Clamp (frameNumber, fightAnimationMin, fightAnimationMax + 1); spriteScalerY = 0.9150f; spriteFightY = -0.009f; if (frameNumber > fightAnimationMax) { // once we have finished the fight animation, detect if we have hit the player again doFightDamage = false; if (state == ControlState.FightingDroid) { // when looping the fight animation it looks better to start at fightAnimationMin + 4 frameNumber = fightAnimationMin + 4; } else { currentAnimation = AnimationLoops.animationWalk; frameNumber = walkAnimationMin; } } // if this droid is within 2 units of the player and if we are at least 4 frames into the animation, and we haven't attacked yet if (state == ControlState.FightingDroid && frameNumber > fightAnimationMin + 4 && doFightDamage == false) { // damage the player doFightDamage = true; if (gameObject != null && objPlayer != null) { playerScript ptrPlayerScript = (playerScript)objPlayer.GetComponent (typeof(playerScript)); ptrPlayerScript.maintenancePoints -= fightDamage; //print ("Attacked by " + gameObject.tag); } } } // manage the animationWave animation if (currentAnimation == AnimationLoops.animationWave) { frameNumber = Mathf.Clamp (frameNumber, waveAnimationMin, waveAnimationMax + 1); spriteScalerY = 0.8918f; spriteFightY = 0f; if (frameNumber > waveAnimationMax) { frameNumber = waveAnimationMin; } } // manage the animationWalk animation if (currentAnimation == AnimationLoops.animationWalk) { frameNumber = Mathf.Clamp (frameNumber, walkAnimationMin, walkAnimationMax + 1); spriteScalerY = 0.9150f; spriteFightY = 0f; if (frameNumber > walkAnimationMax) { frameNumber = walkAnimationMin; } } // reset the animationTime to 1/framerate, so we can begin the local timeline process again animationTime += (1 / animationFrameRate); } // The remaining code finds the image for the animation frame on the sprite sheet, // then displays it on the surface of the droid's plane model as a texture // First, find the number of frames down the animation is on the sprite sheet and set the y coordinate accordingly spriteSheetCount.y = 0; for (i=(int)frameNumber; i > (int)spriteSheetTotalRows; i-=(int)spriteSheetTotalRows) { spriteSheetCount.y += 1; } // Second, find the number of frames down the animation is on the sprite sheet and set the x coordinate accordingly spriteSheetCount.x = i - 1; // Third, calculate the exact X and Y pixel coordinate on the sprite sheet of the frame to display double calcY = spriteScalerY * (spriteSheetCount.y / spriteSheetTotalRows) + spriteFightY; double calcX = 0.9150 * (spriteSheetCount.x / spriteSheetTotalCols) + 0.033; spriteSheetOffset = new Vector2 ((float)(1 - calcX), (float)(1 - calcY)); // Last, offset the texture to display the correct frame renderer.material.SetTextureOffset ("_MainTex", spriteSheetOffset); }