Hey there!
I am pretty newbie on coding games. I have a movement problem with my slime. The slime has 3 animation phases: Idle, Jump and Fall. I wanted this slime wait a little before jumping animation so I put boolean conditions as jumping and falling in animation. I’ve been trying to make this slime moving leftcap to rightcap from its position. However, this slime stops after reached to leftcap point. Also, it seems like it stucks at idle animation when slime reaches to leftcap point. I don’t know why but it does not give any error messages so that I can not understand what is the problem. Here is my code for the slime:
public class Slime : Enemy
{
[SerializeField] private float leftCap;
[SerializeField] private float rightCap;
[SerializeField] private float jumpLength;
[SerializeField] private float jumpHeight;
[SerializeField] private LayerMask ground;
private Collider2D coll;
private Rigidbody2D rb;
private bool facingLeft = true;
protected override void Start()
{
base.Start();
coll = GetComponent<Collider2D>();
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (anim.GetBool("Jumping"))
{
if (rb.velocity.y < 0.1f)
{
anim.SetBool("Jumping", false);
anim.SetBool("Falling", true);
}
}
if (coll.IsTouchingLayers(ground) && anim.GetBool("Falling"))
{
anim.SetBool("Falling", false);
}
}
private void Move()
{
if (facingLeft)
{
if (transform.position.x > leftCap)
{
if (transform.localScale.x != 1)
{
transform.localScale = new Vector3(1, 1);
}
if (coll.IsTouchingLayers(ground))
{
rb.velocity = new Vector2(-jumpLength, jumpHeight);
anim.SetBool("Jumping", true);
}
}
else
{
facingLeft = false;
}
}
else
{
if (transform.position.x < rightCap)
{
if (transform.localScale.x != -1)
{
transform.localScale = new Vector3(-1, 1);
}
if (coll.IsTouchingLayers(ground))
{
rb.velocity = new Vector2(jumpLength, jumpHeight);
anim.SetBool("Jumping", true);
}
}
else
{
facingLeft = true;
}
}
}
}
Thanks for the help! Appreciate it.