Hey, I am new to Unity and right now I have a problem, where I cant figure out the solution.
I am not exactly sure what I did, but yesterday everything was fine. I created a bouncy slime enemy with colliders and everything worked fine.
Today I have done some experiments and now the collisions and the rigidbody arent working anymore.
These are the scripts I use for the slime:
//Start() variables
[SerializeField] private Collider2D coll;
//Finite State Machine
private enum State {standing, jumping}
private State state = State.standing;
public LayerMask ground;
public float leftCap = 8f;
public float rightCap = 19f;
public float jumpLength = 5f;
public float jumpHeight = 5f;
private bool facingRight = true;
protected override void Start()
{
base.Start();
coll = GetComponent<Collider2D>();
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
//Transition from Jump to Fall
if (anim.GetBool("Jumping"))
{
if (rb.velocity.y < .1f)
{
anim.SetBool("Falling", true);
anim.SetBool("Jumping", false);
}
}
//Transition from Fall to standing
if (coll.IsTouchingLayers(ground) && anim.GetBool("Falling"))
{
anim.SetBool("Standing", true);
anim.SetBool("Falling", false);
anim.SetBool("Jumping", false);
}
}
private void Move()
{
if (facingRight)
{
if (transform.position.x < rightCap)
{
//Make sure Sprite faces right location, if not, then face right direction
if (transform.localScale.x != -1 && coll.IsTouchingLayers(ground))
{
transform.localScale = new Vector3(1, 1);
}
//Test to see if the slime is on the ground, if so jump
if (coll.IsTouchingLayers(ground))
{
//Jump
rb.velocity = new Vector2(jumpLength, jumpHeight);
anim.SetBool("Jumping", true);
}
}
else
{
facingRight = false;
}
}
else
{
if (transform.position.x > rightCap)
{
//Make sure Sprite faces right location, if not, then face right direction
if (transform.localScale.x != 1)
{
transform.localScale = new Vector3(-1, 1);
}
//Test to see if the slime is on the ground, if so jump
if (coll.IsTouchingLayers(ground) && coll.IsTouchingLayers(ground))
{
//Jump
rb.velocity = new Vector2(-jumpLength, jumpHeight);
anim.SetBool("Jumping", true);
}
}
else
{
facingRight = true;
}
}
}
[SerializeField] protected Animator anim;
[SerializeField] protected Rigidbody2D rb;
public float PushDownForce = 1f;
protected virtual void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
public void JumpedOn()
{
anim.SetTrigger("Death");
rb.velocity = new Vector2(0, -PushDownForce);
}
private void Death()
{
Destroy(this.gameObject);
}
}
Inside the Inspector it looks like this:
I dont know what is wrong, so it would be really cool, if someone could help me out!