Harald लिखाDid you have a look at the example scenes in the Spine Examples
directory that come with the spine-unity runtime?
I did but it did not help me because of my limited understanding of how these things work :shrug:
Harald लिखाIt's not quite clear what was shown in your Think Citric tutorial, what worked before adding which lines. Please note that you will need to learn programming first when writing any behavior scripts in Unity. You will get frustrated when trying to create a patchwork of copy&paste code snippets from various tutorial sources without understanding how and why things work.
Well, it was a tutorial and I was watching it to learn, I believe that's why I even managed to get some of my code to work.
I've highlighted my own lines with the # symbol, I hope it helps.
Before I added my lines, everything worked correctly - the character played the correct walk and idle animations, and switched them based on the specified conditions, i.e if the movement float variable is greater than 0 it would play the walk animation, if it equals zero, it would play idle.
So, I tried to follow the same principle and add a run and runspeed float variables. If run is greater than 0, play the run animation (which it does). But since my character does not move forward, I can only assume something is wrong with these two lines (they are inside the Move method).
run = Input.GetAxis("Vertical");
rigidbody.velocity = new Vector2(run * runspeed, rigidbody.velocity.y);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;
public class Klym : MonoBehaviour
{
public SkeletonAnimation skeletonAnimation;
public AnimationReferenceAsset idle, walking, running;
public string currentState;
public float speed;
public float movement;
public string currentAnimation;
#public float runspeed;
#public float run;
private Rigidbody2D rigidbody;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
currentState = "Idle";
SetCharacterState(currentState);
}
// Update is called once per frame
void Update()
{
Move();
}
//sets character animation
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;
}
//checks character state and sets the animation accordingly
public void SetCharacterState(string state)
{
if (state.Equals("Walking"))
{
SetAnimation(walking, true, 1f);
}
#else if(state.Equals("Running"))
#{
# SetAnimation(running, true, 1f);
#}
else if(state.Equals("Idle"))
{
SetAnimation(idle, true, 1f);
}
}
public void Move()
{
# run = Input.GetAxis("Vertical");
# rigidbody.velocity = new Vector2(run * runspeed, rigidbody.velocity.y);
movement = Input.GetAxis("Horizontal");
rigidbody.velocity = new Vector2(movement * speed, rigidbody.velocity.y);
if (movement != 0)
{
SetCharacterState("Walking");
if (movement > 0)
{
transform.localScale = new Vector2(0.11f, 0.11f);
}
else if (movement < 0)
{
transform.localScale = new Vector2(-0.11f, 0.11f);
}
}
# else if (run != 0)
#{
# SetCharacterState("Running");
# if (run > 0)
# {
# transform.localScale = new Vector2(0.11f, 0.11f);
# }
# else if (run < 0)
# {
# transform.localScale = new Vector2(-0.11f, 0.11f);
# }
# }
else
{
SetCharacterState("Idle");
}
}
}
Harald लिखाWhat does your animation look like, is it an on-location animation without any horizontal movement (no root motion), or does it move horizontally and then repeat from the start again after each loop?
If I understand correctly, it's a treadmill animation and I'm using code to move the character horizontally. The animation is played correctly, it's just that the character does not actually move forward. Should I record a video of what it looks like?
Nevermind, I seem to have solved it
Replaced the line
rigidbody.velocity = new Vector2(run * runspeed, rigidbody.velocity.y);
with
transform.Translate(Vector2.right * run * runspeed * Time.deltaTime);
All good now.