Unity - Animations - Animation Doesn't Flip Left/Right
I have code that works for flipping the player to face the direction that it is moving upon the X-axis. However, since I have added animations, the animation will play, but will not flip in the other direction.
Notes-
- I'm not accessing an Animator to run my animations
- I created AnimationClip variables
- I play my animations GetComponent().Play("Idle");
- I do have an Animation component added to my player controller with
fields for each animation inserted.
Code
[SerializeField]
private AnimationClip idle;
[SerializeField]
private AnimationClip walk;
[SerializeField]
private AnimationClip hop; // Alternative name for JUMP
[SerializeField]
private AnimationClip death;
- The code I have below works perfectly, However, making a transition to Jump has proven difficult.
Code
if (Mathf.Abs(player.velocity.x) > 0)
{
GetComponent<Animation>().Play("Walk");
}
else
{
GetComponent<Animation>().Play("Idle");
}
- The code below for jump DOESN'T play the animation:
Code
if (Input.GetKeyDown(KeyCode.Space) && onFloor==true ||
Input.GetKeyDown(KeyCode.W) && onFloor==true)
{
player.velocity = new Vector2(0, jump);
onFloor = false;
// GetComponent<Animation>().Stop("Walk");
// GetComponent<Animation>().Stop("Idle");
GetComponent<Animation>().Play("Jump");
}
- I tried to stop all other animations and only play the jump
animation. - None of that worked. Why? And what would be a solution?
- My animation clips will play but won't flip to the left.
Code
faceRight = !faceRight;
Vector3 Pscale = transform.localScale;
Pscale.x *= -1;
transform.localScale = Pscale;
- I have tried inserting the animation for walking inside the Pflip()
which is what you're seeing above, a function that's called inside
of a FixedUpdate().

I've tried to set up the Animator State Machine but all animations would occur at the same time. That's when I abandoned the Animator and decided to call my animations to play from code. I feel as though I've set up the state machine properly as far as my transition lines are concerned.
Ok, so I've set a Trigger for each transition line named "Activate". I revamped my code accordingly into this:
private Animator anim;
void Start()
{
anim = gameObject.GetComponent<Animator>();
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
//ani.SetTrigger(walk.ToString());
anim.ResetTrigger("Idle");
anim.SetTrigger("Walk");
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
//ani.SetTrigger(idle.ToString());
anim.ResetTrigger("Walk");
anim.SetTrigger("Idle");
}
However, this still doesn't fix my issues. For one, the animations do not play. Secondly, I cannot test to see whether or not my player will flip on the axis once playing.
I'm getting a mild error in the console during runtime that say's the animations don't exist. I made sure that all animations are on the states inside of the animator. I also made sure that none of the animations are marked Legacy.
c# unity3d
|
show 1 more comment
I have code that works for flipping the player to face the direction that it is moving upon the X-axis. However, since I have added animations, the animation will play, but will not flip in the other direction.
Notes-
- I'm not accessing an Animator to run my animations
- I created AnimationClip variables
- I play my animations GetComponent().Play("Idle");
- I do have an Animation component added to my player controller with
fields for each animation inserted.
Code
[SerializeField]
private AnimationClip idle;
[SerializeField]
private AnimationClip walk;
[SerializeField]
private AnimationClip hop; // Alternative name for JUMP
[SerializeField]
private AnimationClip death;
- The code I have below works perfectly, However, making a transition to Jump has proven difficult.
Code
if (Mathf.Abs(player.velocity.x) > 0)
{
GetComponent<Animation>().Play("Walk");
}
else
{
GetComponent<Animation>().Play("Idle");
}
- The code below for jump DOESN'T play the animation:
Code
if (Input.GetKeyDown(KeyCode.Space) && onFloor==true ||
Input.GetKeyDown(KeyCode.W) && onFloor==true)
{
player.velocity = new Vector2(0, jump);
onFloor = false;
// GetComponent<Animation>().Stop("Walk");
// GetComponent<Animation>().Stop("Idle");
GetComponent<Animation>().Play("Jump");
}
- I tried to stop all other animations and only play the jump
animation. - None of that worked. Why? And what would be a solution?
- My animation clips will play but won't flip to the left.
Code
faceRight = !faceRight;
Vector3 Pscale = transform.localScale;
Pscale.x *= -1;
transform.localScale = Pscale;
- I have tried inserting the animation for walking inside the Pflip()
which is what you're seeing above, a function that's called inside
of a FixedUpdate().

I've tried to set up the Animator State Machine but all animations would occur at the same time. That's when I abandoned the Animator and decided to call my animations to play from code. I feel as though I've set up the state machine properly as far as my transition lines are concerned.
Ok, so I've set a Trigger for each transition line named "Activate". I revamped my code accordingly into this:
private Animator anim;
void Start()
{
anim = gameObject.GetComponent<Animator>();
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
//ani.SetTrigger(walk.ToString());
anim.ResetTrigger("Idle");
anim.SetTrigger("Walk");
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
//ani.SetTrigger(idle.ToString());
anim.ResetTrigger("Walk");
anim.SetTrigger("Idle");
}
However, this still doesn't fix my issues. For one, the animations do not play. Secondly, I cannot test to see whether or not my player will flip on the axis once playing.
I'm getting a mild error in the console during runtime that say's the animations don't exist. I made sure that all animations are on the states inside of the animator. I also made sure that none of the animations are marked Legacy.
c# unity3d
So does your jump animation work or not? You say it doesn't in point 6 but you say the clips (which clip?) works in point 9. Your flipping code was taken from the Unity's example, which probably means that its the way you are calling it that is wrong.
– SwiftingDuster
Nov 25 '18 at 3:46
@SwiftingDuster No, the Jump animation will not play. I thought it was because one of the other animations were playing and overriding the animation. Which is when I tried to stop the other animations from playing before running it. However, this did not work.
– AntonioTorro
Nov 25 '18 at 4:10
Ok, the code that works is the walk/Idle. The code that doesn't work is Jump.
– AntonioTorro
Nov 25 '18 at 4:11
Ugh.. I confused myself and you. All my code works. However, when I implemented the animations, the code still works, but the jump animation does not. Additionally, the player no longer will face left. The code I provided for flipping DOES work without the animation code attached. The walk and idle animations do work; however, the flipping part stopped working. Make sense? And sorry for the confusion.
– AntonioTorro
Nov 25 '18 at 4:14
I would recommend you to use Animator Controller. It will help you a lot to make transitions between one animation an another. Check this: docs.unity3d.com/Manual/class-AnimatorController.html If you face problems with the animator, let us know here, we will try to help.
– Ignacio Alorre
Nov 25 '18 at 6:31
|
show 1 more comment
I have code that works for flipping the player to face the direction that it is moving upon the X-axis. However, since I have added animations, the animation will play, but will not flip in the other direction.
Notes-
- I'm not accessing an Animator to run my animations
- I created AnimationClip variables
- I play my animations GetComponent().Play("Idle");
- I do have an Animation component added to my player controller with
fields for each animation inserted.
Code
[SerializeField]
private AnimationClip idle;
[SerializeField]
private AnimationClip walk;
[SerializeField]
private AnimationClip hop; // Alternative name for JUMP
[SerializeField]
private AnimationClip death;
- The code I have below works perfectly, However, making a transition to Jump has proven difficult.
Code
if (Mathf.Abs(player.velocity.x) > 0)
{
GetComponent<Animation>().Play("Walk");
}
else
{
GetComponent<Animation>().Play("Idle");
}
- The code below for jump DOESN'T play the animation:
Code
if (Input.GetKeyDown(KeyCode.Space) && onFloor==true ||
Input.GetKeyDown(KeyCode.W) && onFloor==true)
{
player.velocity = new Vector2(0, jump);
onFloor = false;
// GetComponent<Animation>().Stop("Walk");
// GetComponent<Animation>().Stop("Idle");
GetComponent<Animation>().Play("Jump");
}
- I tried to stop all other animations and only play the jump
animation. - None of that worked. Why? And what would be a solution?
- My animation clips will play but won't flip to the left.
Code
faceRight = !faceRight;
Vector3 Pscale = transform.localScale;
Pscale.x *= -1;
transform.localScale = Pscale;
- I have tried inserting the animation for walking inside the Pflip()
which is what you're seeing above, a function that's called inside
of a FixedUpdate().

I've tried to set up the Animator State Machine but all animations would occur at the same time. That's when I abandoned the Animator and decided to call my animations to play from code. I feel as though I've set up the state machine properly as far as my transition lines are concerned.
Ok, so I've set a Trigger for each transition line named "Activate". I revamped my code accordingly into this:
private Animator anim;
void Start()
{
anim = gameObject.GetComponent<Animator>();
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
//ani.SetTrigger(walk.ToString());
anim.ResetTrigger("Idle");
anim.SetTrigger("Walk");
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
//ani.SetTrigger(idle.ToString());
anim.ResetTrigger("Walk");
anim.SetTrigger("Idle");
}
However, this still doesn't fix my issues. For one, the animations do not play. Secondly, I cannot test to see whether or not my player will flip on the axis once playing.
I'm getting a mild error in the console during runtime that say's the animations don't exist. I made sure that all animations are on the states inside of the animator. I also made sure that none of the animations are marked Legacy.
c# unity3d
I have code that works for flipping the player to face the direction that it is moving upon the X-axis. However, since I have added animations, the animation will play, but will not flip in the other direction.
Notes-
- I'm not accessing an Animator to run my animations
- I created AnimationClip variables
- I play my animations GetComponent().Play("Idle");
- I do have an Animation component added to my player controller with
fields for each animation inserted.
Code
[SerializeField]
private AnimationClip idle;
[SerializeField]
private AnimationClip walk;
[SerializeField]
private AnimationClip hop; // Alternative name for JUMP
[SerializeField]
private AnimationClip death;
- The code I have below works perfectly, However, making a transition to Jump has proven difficult.
Code
if (Mathf.Abs(player.velocity.x) > 0)
{
GetComponent<Animation>().Play("Walk");
}
else
{
GetComponent<Animation>().Play("Idle");
}
- The code below for jump DOESN'T play the animation:
Code
if (Input.GetKeyDown(KeyCode.Space) && onFloor==true ||
Input.GetKeyDown(KeyCode.W) && onFloor==true)
{
player.velocity = new Vector2(0, jump);
onFloor = false;
// GetComponent<Animation>().Stop("Walk");
// GetComponent<Animation>().Stop("Idle");
GetComponent<Animation>().Play("Jump");
}
- I tried to stop all other animations and only play the jump
animation. - None of that worked. Why? And what would be a solution?
- My animation clips will play but won't flip to the left.
Code
faceRight = !faceRight;
Vector3 Pscale = transform.localScale;
Pscale.x *= -1;
transform.localScale = Pscale;
- I have tried inserting the animation for walking inside the Pflip()
which is what you're seeing above, a function that's called inside
of a FixedUpdate().

I've tried to set up the Animator State Machine but all animations would occur at the same time. That's when I abandoned the Animator and decided to call my animations to play from code. I feel as though I've set up the state machine properly as far as my transition lines are concerned.
Ok, so I've set a Trigger for each transition line named "Activate". I revamped my code accordingly into this:
private Animator anim;
void Start()
{
anim = gameObject.GetComponent<Animator>();
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
//ani.SetTrigger(walk.ToString());
anim.ResetTrigger("Idle");
anim.SetTrigger("Walk");
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
//ani.SetTrigger(idle.ToString());
anim.ResetTrigger("Walk");
anim.SetTrigger("Idle");
}
However, this still doesn't fix my issues. For one, the animations do not play. Secondly, I cannot test to see whether or not my player will flip on the axis once playing.
I'm getting a mild error in the console during runtime that say's the animations don't exist. I made sure that all animations are on the states inside of the animator. I also made sure that none of the animations are marked Legacy.
c# unity3d
c# unity3d
edited Nov 25 '18 at 21:53
AntonioTorro
asked Nov 25 '18 at 3:17
AntonioTorroAntonioTorro
8510
8510
So does your jump animation work or not? You say it doesn't in point 6 but you say the clips (which clip?) works in point 9. Your flipping code was taken from the Unity's example, which probably means that its the way you are calling it that is wrong.
– SwiftingDuster
Nov 25 '18 at 3:46
@SwiftingDuster No, the Jump animation will not play. I thought it was because one of the other animations were playing and overriding the animation. Which is when I tried to stop the other animations from playing before running it. However, this did not work.
– AntonioTorro
Nov 25 '18 at 4:10
Ok, the code that works is the walk/Idle. The code that doesn't work is Jump.
– AntonioTorro
Nov 25 '18 at 4:11
Ugh.. I confused myself and you. All my code works. However, when I implemented the animations, the code still works, but the jump animation does not. Additionally, the player no longer will face left. The code I provided for flipping DOES work without the animation code attached. The walk and idle animations do work; however, the flipping part stopped working. Make sense? And sorry for the confusion.
– AntonioTorro
Nov 25 '18 at 4:14
I would recommend you to use Animator Controller. It will help you a lot to make transitions between one animation an another. Check this: docs.unity3d.com/Manual/class-AnimatorController.html If you face problems with the animator, let us know here, we will try to help.
– Ignacio Alorre
Nov 25 '18 at 6:31
|
show 1 more comment
So does your jump animation work or not? You say it doesn't in point 6 but you say the clips (which clip?) works in point 9. Your flipping code was taken from the Unity's example, which probably means that its the way you are calling it that is wrong.
– SwiftingDuster
Nov 25 '18 at 3:46
@SwiftingDuster No, the Jump animation will not play. I thought it was because one of the other animations were playing and overriding the animation. Which is when I tried to stop the other animations from playing before running it. However, this did not work.
– AntonioTorro
Nov 25 '18 at 4:10
Ok, the code that works is the walk/Idle. The code that doesn't work is Jump.
– AntonioTorro
Nov 25 '18 at 4:11
Ugh.. I confused myself and you. All my code works. However, when I implemented the animations, the code still works, but the jump animation does not. Additionally, the player no longer will face left. The code I provided for flipping DOES work without the animation code attached. The walk and idle animations do work; however, the flipping part stopped working. Make sense? And sorry for the confusion.
– AntonioTorro
Nov 25 '18 at 4:14
I would recommend you to use Animator Controller. It will help you a lot to make transitions between one animation an another. Check this: docs.unity3d.com/Manual/class-AnimatorController.html If you face problems with the animator, let us know here, we will try to help.
– Ignacio Alorre
Nov 25 '18 at 6:31
So does your jump animation work or not? You say it doesn't in point 6 but you say the clips (which clip?) works in point 9. Your flipping code was taken from the Unity's example, which probably means that its the way you are calling it that is wrong.
– SwiftingDuster
Nov 25 '18 at 3:46
So does your jump animation work or not? You say it doesn't in point 6 but you say the clips (which clip?) works in point 9. Your flipping code was taken from the Unity's example, which probably means that its the way you are calling it that is wrong.
– SwiftingDuster
Nov 25 '18 at 3:46
@SwiftingDuster No, the Jump animation will not play. I thought it was because one of the other animations were playing and overriding the animation. Which is when I tried to stop the other animations from playing before running it. However, this did not work.
– AntonioTorro
Nov 25 '18 at 4:10
@SwiftingDuster No, the Jump animation will not play. I thought it was because one of the other animations were playing and overriding the animation. Which is when I tried to stop the other animations from playing before running it. However, this did not work.
– AntonioTorro
Nov 25 '18 at 4:10
Ok, the code that works is the walk/Idle. The code that doesn't work is Jump.
– AntonioTorro
Nov 25 '18 at 4:11
Ok, the code that works is the walk/Idle. The code that doesn't work is Jump.
– AntonioTorro
Nov 25 '18 at 4:11
Ugh.. I confused myself and you. All my code works. However, when I implemented the animations, the code still works, but the jump animation does not. Additionally, the player no longer will face left. The code I provided for flipping DOES work without the animation code attached. The walk and idle animations do work; however, the flipping part stopped working. Make sense? And sorry for the confusion.
– AntonioTorro
Nov 25 '18 at 4:14
Ugh.. I confused myself and you. All my code works. However, when I implemented the animations, the code still works, but the jump animation does not. Additionally, the player no longer will face left. The code I provided for flipping DOES work without the animation code attached. The walk and idle animations do work; however, the flipping part stopped working. Make sense? And sorry for the confusion.
– AntonioTorro
Nov 25 '18 at 4:14
I would recommend you to use Animator Controller. It will help you a lot to make transitions between one animation an another. Check this: docs.unity3d.com/Manual/class-AnimatorController.html If you face problems with the animator, let us know here, we will try to help.
– Ignacio Alorre
Nov 25 '18 at 6:31
I would recommend you to use Animator Controller. It will help you a lot to make transitions between one animation an another. Check this: docs.unity3d.com/Manual/class-AnimatorController.html If you face problems with the animator, let us know here, we will try to help.
– Ignacio Alorre
Nov 25 '18 at 6:31
|
show 1 more comment
1 Answer
1
active
oldest
votes
Based on your question and comments.
I've tried to set up the Animator State Machine but all animations would occur at the same time.
This probably means you are setting wrong the transitions or the initial values in the State Machine.
My recommendation for you is to check this video about how to use State Machine. IT belongs to a longer tutorial, but that video may be enough for your problem.
If you follow the tutorial you will see you need to create parameters of type boolean to activate animations from your script. So for example you will create one named isWalking which will be linked to your walk animation. Then from your code you can set that parameter to true or false with:
anim.SetBool ("IsWalking", walking);
This is an example of what you can try. But you will need to adapt it to your real needs:
bool walking = false;
void FixedUpdate ()
{
// Animate the player.
Animating();
}
void Animating (float h, float v)
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
walking = true;
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
walking = false;
}
// Tell the animator whether or not the player is walking.
anim.SetBool ("IsWalking", walking);
}
I'm going to give this a try and I'll let you know if I run into any issues. I appreciate the suggestion! This looks good! I don't understand much about the state machine so your tutorial will be a gold mine of information! You are correct. The problem is that all the states try to activate at the same time and even when I set parameters of IsTrigger or SetBool. This is what's really confusing me. I don't understand why it's happening.
– AntonioTorro
Dec 3 '18 at 0:44
1
Apparently the issue with the player not flipping was directly correlated with the state machine running all states. Once I got the state machine operating properly, the rest of the code fell into place. I had to, as you suggested, make modifications to the code to suit my needs. However, you saved my cyber-designer life lol Great video, thanks for sharing!
– AntonioTorro
Dec 3 '18 at 2:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464345%2funity-animations-animation-doesnt-flip-left-right%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on your question and comments.
I've tried to set up the Animator State Machine but all animations would occur at the same time.
This probably means you are setting wrong the transitions or the initial values in the State Machine.
My recommendation for you is to check this video about how to use State Machine. IT belongs to a longer tutorial, but that video may be enough for your problem.
If you follow the tutorial you will see you need to create parameters of type boolean to activate animations from your script. So for example you will create one named isWalking which will be linked to your walk animation. Then from your code you can set that parameter to true or false with:
anim.SetBool ("IsWalking", walking);
This is an example of what you can try. But you will need to adapt it to your real needs:
bool walking = false;
void FixedUpdate ()
{
// Animate the player.
Animating();
}
void Animating (float h, float v)
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
walking = true;
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
walking = false;
}
// Tell the animator whether or not the player is walking.
anim.SetBool ("IsWalking", walking);
}
I'm going to give this a try and I'll let you know if I run into any issues. I appreciate the suggestion! This looks good! I don't understand much about the state machine so your tutorial will be a gold mine of information! You are correct. The problem is that all the states try to activate at the same time and even when I set parameters of IsTrigger or SetBool. This is what's really confusing me. I don't understand why it's happening.
– AntonioTorro
Dec 3 '18 at 0:44
1
Apparently the issue with the player not flipping was directly correlated with the state machine running all states. Once I got the state machine operating properly, the rest of the code fell into place. I had to, as you suggested, make modifications to the code to suit my needs. However, you saved my cyber-designer life lol Great video, thanks for sharing!
– AntonioTorro
Dec 3 '18 at 2:52
add a comment |
Based on your question and comments.
I've tried to set up the Animator State Machine but all animations would occur at the same time.
This probably means you are setting wrong the transitions or the initial values in the State Machine.
My recommendation for you is to check this video about how to use State Machine. IT belongs to a longer tutorial, but that video may be enough for your problem.
If you follow the tutorial you will see you need to create parameters of type boolean to activate animations from your script. So for example you will create one named isWalking which will be linked to your walk animation. Then from your code you can set that parameter to true or false with:
anim.SetBool ("IsWalking", walking);
This is an example of what you can try. But you will need to adapt it to your real needs:
bool walking = false;
void FixedUpdate ()
{
// Animate the player.
Animating();
}
void Animating (float h, float v)
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
walking = true;
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
walking = false;
}
// Tell the animator whether or not the player is walking.
anim.SetBool ("IsWalking", walking);
}
I'm going to give this a try and I'll let you know if I run into any issues. I appreciate the suggestion! This looks good! I don't understand much about the state machine so your tutorial will be a gold mine of information! You are correct. The problem is that all the states try to activate at the same time and even when I set parameters of IsTrigger or SetBool. This is what's really confusing me. I don't understand why it's happening.
– AntonioTorro
Dec 3 '18 at 0:44
1
Apparently the issue with the player not flipping was directly correlated with the state machine running all states. Once I got the state machine operating properly, the rest of the code fell into place. I had to, as you suggested, make modifications to the code to suit my needs. However, you saved my cyber-designer life lol Great video, thanks for sharing!
– AntonioTorro
Dec 3 '18 at 2:52
add a comment |
Based on your question and comments.
I've tried to set up the Animator State Machine but all animations would occur at the same time.
This probably means you are setting wrong the transitions or the initial values in the State Machine.
My recommendation for you is to check this video about how to use State Machine. IT belongs to a longer tutorial, but that video may be enough for your problem.
If you follow the tutorial you will see you need to create parameters of type boolean to activate animations from your script. So for example you will create one named isWalking which will be linked to your walk animation. Then from your code you can set that parameter to true or false with:
anim.SetBool ("IsWalking", walking);
This is an example of what you can try. But you will need to adapt it to your real needs:
bool walking = false;
void FixedUpdate ()
{
// Animate the player.
Animating();
}
void Animating (float h, float v)
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
walking = true;
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
walking = false;
}
// Tell the animator whether or not the player is walking.
anim.SetBool ("IsWalking", walking);
}
Based on your question and comments.
I've tried to set up the Animator State Machine but all animations would occur at the same time.
This probably means you are setting wrong the transitions or the initial values in the State Machine.
My recommendation for you is to check this video about how to use State Machine. IT belongs to a longer tutorial, but that video may be enough for your problem.
If you follow the tutorial you will see you need to create parameters of type boolean to activate animations from your script. So for example you will create one named isWalking which will be linked to your walk animation. Then from your code you can set that parameter to true or false with:
anim.SetBool ("IsWalking", walking);
This is an example of what you can try. But you will need to adapt it to your real needs:
bool walking = false;
void FixedUpdate ()
{
// Animate the player.
Animating();
}
void Animating (float h, float v)
{
if (Mathf.Abs(player.velocity.x) > 0 && onFloor==true)
{
walking = true;
}
else if(Mathf.Abs(player.velocity.x) == 0 && onFloor==true)
{
walking = false;
}
// Tell the animator whether or not the player is walking.
anim.SetBool ("IsWalking", walking);
}
answered Nov 26 '18 at 7:26
Ignacio AlorreIgnacio Alorre
3,02242747
3,02242747
I'm going to give this a try and I'll let you know if I run into any issues. I appreciate the suggestion! This looks good! I don't understand much about the state machine so your tutorial will be a gold mine of information! You are correct. The problem is that all the states try to activate at the same time and even when I set parameters of IsTrigger or SetBool. This is what's really confusing me. I don't understand why it's happening.
– AntonioTorro
Dec 3 '18 at 0:44
1
Apparently the issue with the player not flipping was directly correlated with the state machine running all states. Once I got the state machine operating properly, the rest of the code fell into place. I had to, as you suggested, make modifications to the code to suit my needs. However, you saved my cyber-designer life lol Great video, thanks for sharing!
– AntonioTorro
Dec 3 '18 at 2:52
add a comment |
I'm going to give this a try and I'll let you know if I run into any issues. I appreciate the suggestion! This looks good! I don't understand much about the state machine so your tutorial will be a gold mine of information! You are correct. The problem is that all the states try to activate at the same time and even when I set parameters of IsTrigger or SetBool. This is what's really confusing me. I don't understand why it's happening.
– AntonioTorro
Dec 3 '18 at 0:44
1
Apparently the issue with the player not flipping was directly correlated with the state machine running all states. Once I got the state machine operating properly, the rest of the code fell into place. I had to, as you suggested, make modifications to the code to suit my needs. However, you saved my cyber-designer life lol Great video, thanks for sharing!
– AntonioTorro
Dec 3 '18 at 2:52
I'm going to give this a try and I'll let you know if I run into any issues. I appreciate the suggestion! This looks good! I don't understand much about the state machine so your tutorial will be a gold mine of information! You are correct. The problem is that all the states try to activate at the same time and even when I set parameters of IsTrigger or SetBool. This is what's really confusing me. I don't understand why it's happening.
– AntonioTorro
Dec 3 '18 at 0:44
I'm going to give this a try and I'll let you know if I run into any issues. I appreciate the suggestion! This looks good! I don't understand much about the state machine so your tutorial will be a gold mine of information! You are correct. The problem is that all the states try to activate at the same time and even when I set parameters of IsTrigger or SetBool. This is what's really confusing me. I don't understand why it's happening.
– AntonioTorro
Dec 3 '18 at 0:44
1
1
Apparently the issue with the player not flipping was directly correlated with the state machine running all states. Once I got the state machine operating properly, the rest of the code fell into place. I had to, as you suggested, make modifications to the code to suit my needs. However, you saved my cyber-designer life lol Great video, thanks for sharing!
– AntonioTorro
Dec 3 '18 at 2:52
Apparently the issue with the player not flipping was directly correlated with the state machine running all states. Once I got the state machine operating properly, the rest of the code fell into place. I had to, as you suggested, make modifications to the code to suit my needs. However, you saved my cyber-designer life lol Great video, thanks for sharing!
– AntonioTorro
Dec 3 '18 at 2:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464345%2funity-animations-animation-doesnt-flip-left-right%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So does your jump animation work or not? You say it doesn't in point 6 but you say the clips (which clip?) works in point 9. Your flipping code was taken from the Unity's example, which probably means that its the way you are calling it that is wrong.
– SwiftingDuster
Nov 25 '18 at 3:46
@SwiftingDuster No, the Jump animation will not play. I thought it was because one of the other animations were playing and overriding the animation. Which is when I tried to stop the other animations from playing before running it. However, this did not work.
– AntonioTorro
Nov 25 '18 at 4:10
Ok, the code that works is the walk/Idle. The code that doesn't work is Jump.
– AntonioTorro
Nov 25 '18 at 4:11
Ugh.. I confused myself and you. All my code works. However, when I implemented the animations, the code still works, but the jump animation does not. Additionally, the player no longer will face left. The code I provided for flipping DOES work without the animation code attached. The walk and idle animations do work; however, the flipping part stopped working. Make sense? And sorry for the confusion.
– AntonioTorro
Nov 25 '18 at 4:14
I would recommend you to use Animator Controller. It will help you a lot to make transitions between one animation an another. Check this: docs.unity3d.com/Manual/class-AnimatorController.html If you face problems with the animator, let us know here, we will try to help.
– Ignacio Alorre
Nov 25 '18 at 6:31