- Home /
A problem regarding to player animation with checkpoint in 2D platformer
Hello, I am new to Unity. And I'm currently trying to make a 2D platformer game. Now I had a question. My character has death sound effects and death animation when it touches a trap or falls into holes. At first the animation is demonstrated and the sound effect can be heard, but until I add the checkpoint into the game, my character just disappear for a while then go back to respawn point when it touches trap or drop into hole without death animation but with death sound.
And here are the scripts
Move.cs attached to the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Move : MonoBehaviour
{
// Start is called before the first frame update
private Rigidbody2D rb;
private SpriteRenderer sprite;
private Animator anim;
private BoxCollider2D coll;
public Vector2 respawn;
public LevelManager levelmanager;
[SerializeField] private LayerMask jumpableGround;
private float x = 0f;
[SerializeField]private float spd = 12f;
[SerializeField]private float jump = 12f;
private enum MovementState { idle, walk, jump,fall }
[SerializeField] private AudioSource jumpsound;
void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<BoxCollider2D>();
sprite = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
respawn = transform.position;
levelmanager = FindObjectOfType<LevelManager>();
}
// Update is called once per frame
void Update()
{
x = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(x * spd, rb.velocity.y);
if (Input.GetButtonDown("Jump") && IsGround())
{
rb.velocity = new Vector2 (rb.velocity.x, jump);
jumpsound.Play();
}
UpdateAnimation();
}
private void UpdateAnimation()
{
MovementState state;
if (x > 0f)
{
state = MovementState.walk;
sprite.flipX = false;
}
else if (x < 0f)
{
state = MovementState.walk;
sprite.flipX = true;
}
else
{
state = MovementState.idle;
}
if( rb.velocity.y > .1f )
{
state = MovementState.jump;
}
else if (rb.velocity.y < -.1f)
{
state = MovementState.fall;
}
anim.SetInteger("state", (int)state);
}
private bool IsGround()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag("Fall"))
{
levelmanager.Respawn();
}
if (other.gameObject.CompareTag("Trap"))
{
levelmanager.Respawn();
}
if (other.gameObject.CompareTag("Checkpoint"))
{
respawn = other.transform.position;
}
}
}
Life.cs attached to the player using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class Life : MonoBehaviour
{
public Animator anim;
private Rigidbody2D rb;
public LevelManager levelmanager;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
Hurt();
}
if (collision.gameObject.CompareTag("Fall"))
{
Fall();
}
}
private void Hurt()
{
rb.bodyType = RigidbodyType2D.Static;
levelmanager.Respawn();
anim.SetTrigger("hurt");
}
private void Fall()
{
rb.bodyType = RigidbodyType2D.Static;
anim.SetTrigger("hurt");
}
private void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
LevelManager.cs attached to the Level Manager object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelManager : MonoBehaviour
{
public float respawnDelay;
public Move player;
public SoundEffect sound;
void Start()
{
player = FindObjectOfType<Move>();
sound = FindObjectOfType<SoundEffect>();
}
public void Respawn()
{
sound.death();
StartCoroutine("RespawnCoroutine");
}
public IEnumerator RespawnCoroutine()
{
player.gameObject.SetActive(false);
yield return new WaitForSeconds (respawnDelay);
player.transform.position = player.respawn;
player.gameObject.SetActive(true);
}
}
Idk if it is because of the SetActive is set to false in LevelManager script that makes the death animation stops working. If yes, how do I rewrite my code? Plz help me solve this problem, some explanations to my problem are appreciated.
Your answer
Follow this Question
Related Questions
Animator and Scripting problems. Help !! 1 Answer
Simple Animation and Scripting Problem. 1 Answer
Animation Problems 1 Answer
Why does this extremely simple script return an error? 1 Answer
Rigidbody2D AddForce not working 0 Answers