script doesnt work as intended.
Hey
so ive been working on my custom 2d top down movement, my problem is that the moving animation only works on the y axis.
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
PlayerShoot PlayerShoot;
private Animator anim;
AudioSource audio;
public int health = 40;
public float movespeed = 4.0f;
bool facingRight = true;
public AudioClip KickSound;
void Start ()
{
audio = GetComponent<AudioSource>();
anim = GetComponent<Animator>();
}
void FixedUpdate () {
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
anim.SetFloat("move", Mathf.Abs(x));
GetComponent<Rigidbody2D>().velocity = new Vector3(x * movespeed, y * movespeed, GetComponent<Rigidbody2D>().velocity.y);
anim.SetFloat("move", Mathf.Abs(y));
if (x > 0 && !facingRight)
Flip();
else if (x < 0 && facingRight)
Flip();
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Comment
Best Answer
Answer by Zodiarc · Jun 13, 2016 at 12:52 PM
Well you set only the y value on line 31. You need to set both x and y axes.
im sorry, but setting the x value to line 32 doesnt make any difference.
Oh didn't see the part at line 27. Well what you could do ins$$anonymous$$d is create a vector2 with the x and y value and pass it to anim.SetFloat like that:
Vector2 movement = new Vector2(x, y);
anim.setFloat("move", movement.$$anonymous$$agnitude);