- Home /
Beginner using the animation controller to get a sprite walking left and right..Need some help going from here
So, im trying to get a basic moveleftright and animations to walk left and right for a 2d shooter(magic actually) sidescroll game. Im new and finding it a bit confusing. This is what i have so far.. it works barely but the animation doesnt play long enough and its unresponsive as far as speed to change the animations as well as it occasionally getting stuck on walkLeft for some reason.
My animator controller atm is set up with 1 parameter called state witch is an int. It go's
anystate Entry ->Idle<->walkRight
<->walkLeft so from idle which is the base and back and forth from walk right and left with 2 each. if that makes enough since. so each one of the back to idle is set at 0 walkLeft is set to 2 and walkRight is set to 1. atm no transitions from anystate.
Heres my code so far. You can ignore the mess that is me trying to get jumping working because thats a whole different issue for me atm.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Animator anim;
const int stateIdle = 0;
const int stateWalkleft = 1;
const int stateWalkright = 2;
int _currentAnimationState = stateIdle;
[SerializeField]
private int JumpCount = 0;
[SerializeField]
private int MaxJumps = 2; //max amount you can jump ie 2 for double jump
private bool isGrounded;
[SerializeField]
private float jumpStrength = 20f;
[SerializeField]
private bool dead;
[SerializeField]
private float moveSpeed = 100f;
private Rigidbody2D rb2D;
// Use this for initialization
void Start ()
{
rb2D = GetComponent<Rigidbody2D>();
JumpCount = MaxJumps;
anim = GetComponent<Animator>();
}
private void FixedUpdate()
{
walk();
}
// Update is called once per frame
void Update ()
{
}
private void walk()
{
if ( Input.GetKey("d"))
{
if (dead != true)
rb2D.AddForce(transform.right * moveSpeed * Time.deltaTime);
changeState(stateWalkleft);
}
else if (Input.GetKey("a"))
{
if (dead != true)
rb2D.AddForce(-transform.right * moveSpeed * Time.deltaTime);
changeState(stateWalkright);
}
else if (Input.GetKeyDown("space") == true && isGrounded)
{
if (JumpCount > 0 )
{
rb2D.AddForce(transform.up * jumpStrength );
JumpCount = JumpCount - 1;
}
else if (JumpCount <= 0)
{
}
}
else if (isGrounded)
{
changeState(stateIdle);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
switch (collision.gameObject.tag)
{
case "floor":
{
JumpCount = MaxJumps;
isGrounded = true;
//print("I'm touching the ground");
return;
}
default:
{
isGrounded = false;
return;
}
}
}
private void changeState(int state)
{
if (_currentAnimationState == state)
{
switch (state)
{
case stateWalkleft:
{
anim.SetInteger("state", stateWalkleft);
break;
}
}
switch (state)
{
case stateWalkright:
{
anim.SetInteger("state", stateWalkright);
break;
}
}
switch (state)
{
case stateIdle:
{
anim.SetInteger("state", stateIdle);
break;
}
}
}
_currentAnimationState = state;
}
}
My question is, how to make the transitions fast enough and not get stuck. As well as looping them.
Your answer
Follow this Question
Related Questions
Multiple Animation Controllers 1 Answer
Animator warning in editor, animation is preventing script from working 1 Answer
When i make my character animated, he can only move up, down left and right (2D) (JS) 0 Answers
How to Sync the Running and Attack Animations for a 2D Sprite? 1 Answer
Sprite animation 2 Answers