0
\$\begingroup\$

So, I got this to do the anims for my sprite in Unity 2d...

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {
public float Speed = 2;
Animator anim;
// Use this for initialization
void Start () {
    anim = GetComponent<Animator>();
}

void FixedUpdate () {
    rigidbody2D.velocity = new Vector2(Input.GetAxis("Horizontal") * Speed, 0);
    anim.SetFloat("HorizontalSpeed", Mathf.Abs(rigidbody2D.velocity.x));

if(transform.localScale.x > 0 && rigidbody2D.velocity.x  < 0)
    transform.localScale = new Vector3(-1, 1, 1); 

if(transform.localScale.x < 0 && rigidbody2D.velocity.x > 0)
    transform.localScale = new Vector3(1, 1, 1); 

}
}

That works fine... then I add the script so he can move up and down. (the previous was moving left+right)

using UnityEngine;
using System.Collections;

public class UpMove : MonoBehaviour {
public float VerticalSpeed = 2;
Animator anim;
// Use this for initialization
void Start () {
    anim = GetComponent<Animator>();
}

void FixedUpdate () {
    rigidbody2D.velocity = new Vector2(Input.GetAxis("Vertical") * VerticalSpeed, 0);
    anim.SetFloat("VerticalSpeed", Mathf.Abs(rigidbody2D.velocity.y));
}
}

Welll.. The second script doesn't do anything. I've been working at this (rookie noobness) for around an hour now.. so pls pls pls.

\$\endgroup\$
0
\$\begingroup\$

You're asking for the y property of the Vector2 you created, which is 0. You set the input to vec2.x

Try:

void FixedUpdate () {
    rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, Input.GetAxis("Vertical") * VerticalSpeed);
    anim.SetFloat("VerticalSpeed", Mathf.Abs(rigidbody2D.velocity.y));
}

Do same change for the Horizontal code too. Change new Vector2(...,0) to new Vector2(...,rigidbody2D.velocity.y);

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Hey. thanks - Now the animation changes but the sprite does not move.. Let me double check some things. \$\endgroup\$ – Cole Birky Dec 14 '15 at 15:59
  • \$\begingroup\$ Please explain.. I'm kind of a noob at this stuff. \$\endgroup\$ – Cole Birky Dec 14 '15 at 16:07
  • \$\begingroup\$ You want to set Vertical Speed that relates to the Y axis in coordinate system. That's why I set X of new Vector2 to 0 and set Y according to input. \$\endgroup\$ – Hamza Hasan Dec 14 '15 at 16:09
  • \$\begingroup\$ Or in more simple words. In 2D coordinate system. Left and Right dependent on X-axis while Up and Down belongs to Y-axis \$\endgroup\$ – Hamza Hasan Dec 14 '15 at 16:11
  • \$\begingroup\$ I see. What would I even change for the sprite to move? Because I make the vector2, getting what I'm pressing to go vertical, then it sets my float in the animation to the velocity of the y axis... I don't understand why that wouldn't do anything.. but Idk. The animation works, but the Y of the sprite does not change. \$\endgroup\$ – Cole Birky Dec 14 '15 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.