- Home /
How to change transform.position.x after a state is changed?
When I press attack button, player will change to Normal Attack state to perform Normal Attack animation and you will notice the player seem like step back a little bit as the width of sprite for Normal Attack animation is longer than Idle animation.
To solve this issue. I need to change the transform.position.x =+ 0.5 to move a bit further to match the position of Idle animation stands and transform.position.x =- 0.5 when Normal Attack state done its animation and back to Idle state. How can I perform transform.position.x =- 0.5 when when Normal Attack state done its animation and back to Idle state?
Answer by ChristianLinnell · May 02, 2014 at 04:33 AM
You should not try to do this in code; you should fix your sprites so the player's centre is aligned in all of them.
But if you must, you could set a flag (let's call it justAttacked) to true and move the player when your player presses the attack button, and then in your update function, do this (assuming your attack animation is called attackAnimation:
if(attackAnimation.isPlaying() == false && justAttacked == true){
transform.position.x -= 0.5;
justAttacked = false;
}
What is the animation? Are you changing an image or playing an actual animation?
If you post the script you're using to make the animation happen I can edit it for you.
I use the Animation provided by unity to animate and save it as NormalAttack.anim. Then I assign it into player.controller by using Animator in Normal Attack state. So, when I press the attack button, it will change to Normal Attack state and perform NormalAttack animation.
Here is my script: https://www.dropbox.com/s/oz8c9i7n4x6pahs/PlayerControl.cs