Sliding animation after running
Hi there,
I peeled the Internet and I didn't find any answer. I'm working on a 2D game where I'm doing the player animations. It's a Spine Character and I AM NOT using the New Input System. I'm using KeyCode / Mouse Button to trigger Movement and animations.
My player runs with the LeftShift. I want to do a "braking" animations once he stops running, like the speed of his runs makes him trip a bit before completely stops.
I'm trying to call the function with KeyCodeUp, I see that it kinda works cuz my Debug.Log is appearing, but the animation won't play (see code below).
Can someone please help me ? I've lost too many days on this (I'm a one year old developer, stil struggling with some stuff).
THANKS !
[CODE]
void Start()
{
player = GetComponent<Rigidbody2D>();
skeletonAnimation = GetComponent<SkeletonAnimation>();
currentstate = "Idle";
SetCharacterState(currentstate);
}
// Update is called once per frame
void Update()
{
playerPos = player.position;
temp = Input.mousePosition;
temp = Camera.main.ScreenToWorldPoint(temp);
GetMousPos();
Move();
StartCoroutine(StopRunning());
}
public void SetAnimation(AnimationReferenceAsset animation, bool loop, float timeScale) {
if (animation.name.Equals(currentAnimation))
{
return;
}
skeletonAnimation.state.SetAnimation(0, animation, loop).TimeScale = timeScale;
currentAnimation = animation.name;
}
public void SetCharacterState(string state)
{
if (state.Equals("Idle"))
{
SetAnimation(idle, true, 0.3f);
}
if (state.Equals("Walking"))
{
SetAnimation(walking, true, 1f);
speed = 2;
}
if (state.Equals("WalkingReverse"))
{
SetAnimation(walkingReverse, true, 1f);
speed = 2;
}
// if (state.Equals("Aiming"))
//{
// SetAnimation(aiming, false, 0.5f);
//}
if (state.Equals("Running"))
{
SetAnimation(running, true, 1f);
speed = 4;
}
if (state.Equals("Slide"))
{
SetAnimation(slide, false, 0.5f);
speed =3;
}
}
public void Move()
{
movement = Input.GetAxis("Horizontal");
player.velocity = new Vector2(movement * speed, player.velocity.y);
player.AddForce(player.velocity, ForceMode2D.Force);
//player.position = Vector2.Lerp(player.position, player.position*movement, speed);
if (movement != 0)
{
StartCoroutine(StopRunning());
if (isflipped)
{
if (movement > 0 )
{
if (Input.GetKey(KeyCode.LeftShift))
{
SetCharacterState("Running");
}
else { SetCharacterState("Walking"); }
}
else {
SetCharacterState("WalkingReverse");
}
}
if (!isflipped)
{
if (movement < 0)
{
if (Input.GetKey(KeyCode.LeftShift))
{
SetCharacterState("Running");
}
else { SetCharacterState("Walking"); }
}
else
{
SetCharacterState("WalkingReverse");
}
}
}
else
{
SetCharacterState("Idle");
}
}
private IEnumerator StopRunning()
{
if ( Input.GetKeyUp(KeyCode.LeftShift))
{
SetCharacterState("Slide");
Debug.Log("J'ai arrêté de courir");
yield return new WaitForSeconds(3f);
}
}
public void GetMousPos()
{
if (temp.x > player.transform.position.x)
{
isflipped = true;
}
else
{
isflipped = false;
}
}
[/CODE]
Your answer
Follow this Question
Related Questions
Distance Joint 2D with Kinematic Player 2 Answers
Could someone help me with my 2D game project? 0 Answers
2D Weird Jumping 1 Answer
Ragdoll Movement Script, Help! 0 Answers