Настроил анимацию персонажа. Сначала всё было в порядке,но через пару минут вылез баг. Когда мой персонаж начинал двигаться, его спрайт уменьшался в несколько раз. Стал похож на блоху.
До движения:
После движения:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float JumpHeight;
public float moveSpeed;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle (groundCheck.position,groundCheckRadius,whatIsGround);
}
// Update is called once per frame
void Update ()
{
if (grounded)
{
doubleJumped=false;
}
anim.SetBool ("Grounded", grounded);
if (Input.GetKeyDown (KeyCode.Space) && grounded)
{
//GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
Jump ();
}
if (Input.GetKeyDown (KeyCode.Space) && !doubleJumped && !grounded)
{
//GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
Jump ();
doubleJumped=true;
}
if (Input.GetKey (KeyCode.D)) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey (KeyCode.A)) {
GetComponent<Rigidbody2D>().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
anim.SetFloat ("Speed", Mathf.Abs(GetComponent<Rigidbody2D> ().velocity.x));
if(GetComponent<Rigidbody2D>().velocity.x > 0)
transform.localScale = new Vector3(1f,1f,1f);
else if(GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3(-1f,1f,1f);
}
public void Jump()
{
GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
}
}
Update: После закоментирования этих строчек баг пропадает.
if(GetComponent<Rigidbody2D>().velocity.x > 0)
transform.localScale = new Vector3(1f,1f,1f);else if(GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3(-1f,1f,1f);