- Home /
Handling animations on sprites that involve axis...
I have a 2d sprite with four animation cycles, each 4 sprites long: "MoveUp", "MoveDown", "MoveLeft" and "MoveRight". Now, I am using GetAxis to allow my movement (top-down RPG style) to take place. I am testing this with my MoveUp animation, however the sprite seems to play it when it is moving up or down. Can I not limit this to when it is only moving upwards?
I have an Animator set up with the state WalkUp, that plays that animation when the parameter "Speed" is higher than "0.01".
Here is my script:
using UnityEngine;
using System.Collections;
public class MoveMachine : MonoBehaviour {
public float maxSpeed = 10f;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
float moveLR = Input.GetAxis ("Horizontal");
float moveUD = Input.GetAxis ("Vertical");
anim.SetFloat ("Speed", Mathf.Abs(moveUD));
rigidbody2D.velocity = new Vector2 (moveUD * maxSpeed, rigidbody2D.velocity.y);
rigidbody2D.velocity = new Vector2 (moveLR * maxSpeed, rigidbody2D.velocity.x);
}
}
I need urgent help with this issue as soon as possible please.
Answer by king_ · Mar 30, 2014 at 04:20 PM
what i meant was ,if you want to play the animation only once(like only when going up) ,so make a bool parameter to your jump animation like "public bool isJumping;' know that if a body is jumping then its y axis velocity will be positive, so check if the body's y axis velocity is positive or not.. if its positive then set the isJumping boolean property to true....
if(rigidbody2d.velocity.y > 0.01f)
{
animator.setbool("isJumping",true);
}
if you have any going down animation just know that y axis velocity will be negative while going down
I understand a little, but I still need more assistance. Can you perhaps rewrite the script for me, if it is not an issue? If not, then can you perhaps explain to me using extracts from my script? Oh and please format your code properly, it's a pain to try and understand unformatted code :/ But thanks!
Can you please respond as fast as possible?
Thanks in advance!
Really you should never ask someone to rewrite your script for you (and especially to do so in a snapsnap manner) that is not in the spirit of UA at all. If you didn't get what you need from that (where @king_ has answered your question with a specific code example), then your time, if you are in such a hurry, is going to be better spent reviewing the tutorials.
This is sounding more like 'my homework is due on $$anonymous$$onday'
I am very sorry for my attitude. I was indeed in a hurry, and was getting worried about this project, however I decided to postpone it until I understand 2D in Unity fully. However you may think of it, I am not a child, and my homework is not due on $$anonymous$$onday.
Again, I apologize for my lack of consistency, however @king_'s answer is not something I can understand easily. I have been working with C# for six years, and have been using Unity3D for two years. However, my issue was nothing in movement, it was the animation. I cannot handle this in bools, strictly because all my code relies on floats and axis, and would be a pain to rewrite. $$anonymous$$y movement is fine, all I need is separate animations for the two different values of the axis. Can someone assist me with that?
Oh and without further ado, let us avoid any comment drama. Please do not down-vote my question any further.
pixelnest.io http://www.raywenderlich.com/61532/unity-2d-tutorial-getting-started these two are so good tutorials for you right now ......... two days is enough for these tuts to complete...... if you finish these you'll know the in an outs of unity 2d animation
Your answer
Follow this Question
Related Questions
Custom Pivot Points in Sprite Editor 1 Answer
Scissor tool with 2d sprite 0 Answers
Animation not playing until i move character. 0 Answers
SpriteManager 2 1 Answer