(2D) Movement-Animation Problems
I am pretty new to Unity and coding and I am following a tutorial, I have two different scripts, One for the animations and one for the movement it self. My problem is that when I change direction, the animation continues to loop and since I have no clue how to fix it I went on here to ask for help from the pro people.
Here is my movement script: using UnityEngine; using System.Collections;
public class Player_Movement : MonoBehaviour {
public float speed;
void Update () {
if(Input.GetKey(KeyCode.W)){
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 1) * (speed));
}
if(Input.GetKey(KeyCode.S)){
GetComponent<Rigidbody2D>().AddForce(new Vector2(0 ,-1) * (speed));
}
if(Input.GetKey(KeyCode.D)){
GetComponent<Rigidbody2D>().AddForce(new Vector2(1, 0) * (speed));
}
if(Input.GetKey(KeyCode.A)){
GetComponent<Rigidbody2D>().AddForce(new Vector2(-1, 0) * (speed));
}
}
}
And here is my animation script:
using UnityEngine;
using System.Collections;
public class Player_Animations : MonoBehaviour {
public Animator anim;
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
if(Input.GetKey (KeyCode.W)){
anim.SetBool("Up",true);
anim.SetBool("Down",false);
anim.SetBool("Right",false);
anim.SetBool("Left",false);
}
if(Input.GetKey (KeyCode.S)){
anim.SetBool("Up",false);
anim.SetBool("Down",true);
anim.SetBool("Right",false);
anim.SetBool("Left",false);
}
if(Input.GetKey (KeyCode.D)){
anim.SetBool("Up",false);
anim.SetBool("Down",false);
anim.SetBool("Right",true);
anim.SetBool("Left",false);
}
if(Input.GetKey (KeyCode.A)){
anim.SetBool("Up",false);
anim.SetBool("Down",false);
anim.SetBool("Right",false);
anim.SetBool("Left",true);
}
if(Input.GetKey(KeyCode.W)){
anim.SetBool("Walking_Up",true);
}
else{
anim.SetBool("Walking_Up",false);
}
if(Input.GetKey(KeyCode.S)){
anim.SetBool("Walking_Down",true);
}
else{
anim.SetBool("Walking_Down",false);
}
if(Input.GetKey(KeyCode.D)){
anim.SetBool("Walking_Right",true);
}
else{
anim.SetBool("Walking_Right",false);
}
if(Input.GetKey(KeyCode.A)){
anim.SetBool("Walking_Left",true);
}
else{
anim.SetBool("Walking_Left",false);
}
}
}
As I said they are simple scripts and are probably insufficient but it is the best I can do at the moment.
Also, here is my Animator.
Following the tutorial I set up the Idles to eachother, for example Player_Idle_Down -(Right-true)- Player_Idle_Right. Then the Idles are connected to the correct Walking animation, for example Player_Idle_Left -(Walking_Left-true)- Player_Walking_Left and then back Player_Walking_Left -(Walking_Left-false)- Player_Idle_Left
Again, probably inadequate, but it is the only way I know how to do it.
I hope I've been informative enough so you understand my problem. Also please be gentle with the coding language as I am a noob xD
-Captain
Your answer
Follow this Question
Related Questions
Can you improve my animation/movement code? *Bugs in details 1 Answer
How can I program diferents velocities to a character? 0 Answers
2d object moves only a little then stops 0 Answers
2D ledge grabbing -1 Answers