Vaspix
January 18, 2020, 8:43pm
1
Hi, I was testing my 2D movement and faced a problem:
When the player goes and jumps in one direction, then changes the direction in midair and keeps going, when it falls to the ground, it looks in the opposite (1st) direction for 1 frame, after that the animation starts and everything work as it should.
All that stuff happens in this statement:
anim.enabled = true;
if (Move == 1)
anim.SetInteger("AnimMode", 1);
else if (Move == -1)
anim.SetInteger("AnimMode", 2);
I have a transition to empty Idle box in Animator from start and 2 transitions each from Any state to the WalkRight and WalkLeft boxes.
I don’t know how to describe this problem properly but if you have any thoughts on what should I check in my code / animator or assume what could cause that type of a problem I’ll be happy to hear that.
Vaspix
January 18, 2020, 8:46pm
2
This is the full code of my Player movement if you don’t mind taking your time to find the issue
Rigidbody2D rb;
GroundCollider grColl;
public float runSpeed = 5f, jHeight = 330f;
float Move;
bool Jump;
Animator anim;
SpriteRenderer rend;
[SerializeField] Sprite Right1, Right2, Left1, Left2, rJump1, rJump2, lJump1, lJump2;
float lastPosY;
bool looksRight = true;
GroundCollider leftColl, rightColl;
void Start()
{
rb = GetComponent<Rigidbody2D>();
grColl = GameObject.Find("GroundCollider").GetComponent<GroundCollider>();
rend = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
lastPosY = transform.position.y;
leftColl = GameObject.Find("LeftCollider").GetComponent<GroundCollider>();
rightColl = GameObject.Find("RightCollider").GetComponent<GroundCollider>();
Physics2D.IgnoreLayerCollision(8, 9);
}
void Update()
{
// Check for Move, change Sprite
if (Input.GetKey(KeyCode.D) && !rightColl.isColl)
{
Move = 1;
looksRight = true;
}
else if (Input.GetKey(KeyCode.A) && !leftColl.isColl)
{
Move = -1;
looksRight = false;
}
else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A) || rightColl.isColl || leftColl.isColl)
Move = 0;
// Check for Jump
if (Input.GetKeyDown(KeyCode.W) && grColl)
Jump = true;
else if (Input.GetKeyUp(KeyCode.W))
Jump = false;
// Animation
if (grColl.isColl)
{
/*if (looksRight) // Set Ground Sprite
rend.sprite = Right1;
else
rend.sprite = Left1;*/
if (Move == 0)
anim.enabled = false;
else // // Bug with this statement: walk & jump in one direction, change direction in midair, when it falls it looks in the opposite (1st) direction for 1 frame, then starts walking normally
{
anim.enabled = true;
if (Move == 1)
anim.SetInteger("AnimMode", 1);
else if (Move == -1)
anim.SetInteger("AnimMode", 2);
} // //
}
else
{
anim.enabled = false;
if (lastPosY + 0.01f < transform.position.y) // posY increased
{
if (looksRight)
rend.sprite = rJump1;
else
rend.sprite = lJump1;
lastPosY = transform.position.y;
}
else if (lastPosY - 0.01f > transform.position.y) // posY decreased
{
if (looksRight)
rend.sprite = rJump2;
else
rend.sprite = lJump2;
lastPosY = transform.position.y;
}
}
}
void FixedUpdate()
{
// Move
if (Move != 0)
rb.transform.Translate(runSpeed * Move * Time.deltaTime, 0, 0);
// Jump
if (Jump && grColl.isColl)
{
rb.Sleep();
rb.AddForce(new Vector2(0, jHeight * Time.deltaTime), ForceMode2D.Impulse);
Jump = false;
}
}