Thanks. It helped somewhat🙂
So, I used the 2d controller in the linked thread. I now got it getting my animations like run, idle and jump. But the example you refered to uses the animator. That would be great but the animator cant use the spine animations right? So I have put the setanimations inside the FixedUpdate. Even me, with no code skills realize that this isnt the best idea. While holding the key down it restarts the animation each loop in the Fixedupdate.
So jumping works, This only checks for a keydown and then plays the jump animation as long as you are moving in y direction(If I understood the script correctly)
but moving right and left only shows the first frame.
Does anybody have a simple sollution to check if the animation already is playing and then NOT set it again until another animation is played? Or if that is a stupdi sollution, maybe someone have a better sollution?
Here is the update and fixed update part of the code I pirate coded: (By piratecoding I mean, I have basically no coding skills but I uses someone elses code and manipulate it until it works at least for testing some stuff out. hehe)
void Update()
{
// a minor bit of trickery here. FixedUpdate sets _up to false so to ensure we never miss any jump presses we leave _up
// set to true if it was true the previous frame
_up = _up || Input.GetKeyDown( KeyCode.UpArrow );
_right = Input.GetKey( KeyCode.RightArrow );
_left = Input.GetKey( KeyCode.LeftArrow );
}
void FixedUpdate()
{
// grab our current _velocity to use as a base for all calculations
_velocity = _controller.velocity;
if( _controller.isGrounded )
_velocity.y = 0;
if( _right )
{
normalizedHorizontalSpeed = 1;
if( transform.localScale.x < 0f )
transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z );
if( _controller.isGrounded )
skeletonAnimation.state.SetAnimation(0, "run", true);
}
else if( _left )
{
normalizedHorizontalSpeed = -1;
if( transform.localScale.x > 0f )
transform.localScale = new Vector3( -transform.localScale.x, transform.localScale.y, transform.localScale.z );
if( _controller.isGrounded )
skeletonAnimation.state.SetAnimation(0, "run", true);
}
else
{
normalizedHorizontalSpeed = 0;
if( _controller.isGrounded )
skeletonAnimation.state.AddAnimation(0, "idle", true, 0);
}
// we can only jump whilst grounded
if( _controller.isGrounded && _up )
{
_velocity.y = Mathf.Sqrt( 2f * jumpHeight * -gravity );
skeletonAnimation.state.SetAnimation(0, "jump", true);
}
// apply horizontal speed smoothing it
var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
_velocity.x = Mathf.Lerp( _velocity.x, normalizedHorizontalSpeed * runSpeed, Time.fixedDeltaTime * smoothedMovementFactor );
// apply gravity before moving
_velocity.y += gravity * Time.fixedDeltaTime;
_controller.move( _velocity * Time.fixedDeltaTime );
// reset input
_up = false;
}