2d controller question (script)
I have the sprite animations done for idle, walk left, walk right, jump left, jump right. With bools set to facingRight, isWalking, isJumping connecting the animations. With the jumping connecting to any state. For the script i have this :
public class PlayerMove : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
private bool isGrounded;
// Update is called once per frame
void Update()
{
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
transform.position += move * moveSpeed * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpHeight), ForceMode2D.Force);
GetComponent<Animator>().SetBool("isJumping", true);
StartCoroutine(JumpAnim());
}
if (move.x > 0 && isGrounded)
{
GetComponent<Animator>().SetBool("isWalking", true);
GetComponent<Animator>().SetBool("facingRight", true);
}
if (move.x < 0 && isGrounded)
{
GetComponent<Animator>().SetBool("isWalking", true);
GetComponent<Animator>().SetBool("facingRight", false);
}
if (move.x < 0)
{
GetComponent<Animator>().SetBool("facingRight", false);
}
else if (move.x > 0)
{
GetComponent<Animator>().SetBool("facingRight", true);
}
if (move.x == 0 || isGrounded == false && GetComponent<Animator>().GetBool("isJumping") == false)
{
GetComponent<Animator>().SetBool("isWalking", false);
}
}
IEnumerator JumpAnim()
{
yield return new WaitForSeconds(0.333f);
GetComponent<Animator>().SetBool("isJumping", false);
}
void OnTriggerEnter2D()
{
isGrounded = true;
}
void OnTriggerStay2D()
{
isGrounded = true;
}
void OnTriggerExit2D()
{
isGrounded = false;
}
}
Now the character moves left and right. However the walking animations wont trigger and it does not jump at all. Could use some help.
Your answer
Follow this Question
Related Questions
Ghange the code of the character controller in 3d platformer game kit 0 Answers
How to change direction of RayCast based on camera angle? 0 Answers
Why is my bool being set to true when program is run? 1 Answer
Rigidbody. Acceleration of free falling object works bad 0 Answers
How can I update UI Text score using PlayerPrefs.Getint() on startup? 1 Answer