Hi everyone,
I’m encountering a frustrating issue with my Unity Animator and could use some help. Here’s the situation:
Issue
After my player character dies, a float parameter in the Animator stops updating correctly. This parameter is crucial for managing animations, and it seems to freeze or not respond to changes after the player object is deactivated or destroyed.
- Before Collision: The Animator and float parameters are updating correctly, and everything looks as expected.
- After Collision: The Animator’s float parameter seems to stop updating. The Animator panel starts to look like it’s in edit mode, with two lines showing up where the float parameter should be.
I have tried everything so I hope you guys can help me.
Here is the joystick code:
using System.Collections;
using UnityEngine;
public class JoystickMove : MonoBehaviour
{
public Joystick movementJoystick;
public float playerSpeed = 2.5f; // Default value for playerSpeed
private Rigidbody2D rb;
public Animator animator; // Reference to the Animator component
private Vector2 lastDirection = Vector2.zero;
private void Start()
{
// Start the coroutine to initialize components with a slight delay
Time.timeScale = 1f;
StartCoroutine(InitializeComponents());
}
private void FixedUpdate()
{
if (rb != null && movementJoystick != null)
{
Vector2 direction = movementJoystick.Direction;
HandleMovement(direction);
HandleAnimation(direction);
}
else
{
Debug.LogWarning("Joystick or Rigidbody2D is not assigned.");
}
}
private void HandleMovement(Vector2 direction)
{
if (direction.magnitude > 0.1f)
{
rb.velocity = direction * playerSpeed;
}
else
{
rb.velocity = Vector2.zero;
}
}
private void HandleAnimation(Vector2 direction)
{
// Update animation parameters only when direction changes
if (direction != lastDirection)
{
animator.SetFloat("right", direction.x > 0 ? 1f : 0f);
animator.SetFloat("left", direction.x < 0 ? 1f : 0f);
animator.SetFloat("forwards", direction.y > 0 ? 1f : 0f);
animator.SetFloat("backwards", direction.y < 0 ? 1f : 0f);
lastDirection = direction;
}
}
private IEnumerator InitializeComponents()
{
yield return new WaitForSeconds(0.1f); // Delay for a fraction of a second
GameObject playerObject = GameObject.FindGameObjectWithTag("player");
if (playerObject != null)
{
rb = playerObject.GetComponent<Rigidbody2D>();
animator = playerObject.GetComponent<Animator>();
if (animator != null)
{
animator.SetFloat("right", 0f);
animator.SetFloat("left", 0f);
animator.SetFloat("forwards", 0f);
animator.SetFloat("backwards", 0f);
}
else
{
Debug.LogError("Animator component not found on player object.");
enabled = false; // Disable the script if required components are missing
}
if (rb == null)
{
Debug.LogError("Rigidbody2D component not found on player object.");
enabled = false; // Disable the script if required components are missing
}
}
else
{
Debug.LogError("Player object not found.");
enabled = false; // Disable the script if the player object is missing
}
}
}
Here is the Game over code:
> using System.Collections;
> using System.Collections.Generic;
> using UnityEngine;
> using UnityEngine.SceneManagement;
>
> public class GameOver : MonoBehaviour
> {
> public GameObject gameOverPanel;
> public GameObject joystick;
>
> // Update is called once per frame
> void Update()
> {
> if(GameObject.FindGameObjectWithTag("player") == null)
> {
> GameObject[] meteors = GameObject.FindGameObjectsWithTag("akadáj1");
> foreach (GameObject meteor in meteors)
> {
> Destroy(meteor);
> }
> Time.timeScale = 1f;
> gameOverPanel.SetActive(true);
> joystick.SetActive(false);
> }
> }
>
> public void Restart()
> {
> SceneManager.LoadScene("endlessrun");
> Time.timeScale = 1f;
> }
> public void Menu()
> {
> SceneManager.LoadScene("main menu");
> }
> }
And Here is the Obsticle script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Obstical : MonoBehaviour
{
private GameObject player;
public Loopingback backgroundScript; // Reference to the Loopingback scriptvoid Start() { player = GameObject.FindGameObjectWithTag("player"); // Assuming the background quad has the Loopingback script attached if (backgroundScript == null) { backgroundScript = Object.FindFirstObjectByType<Loopingback>(); // Use the new method } } private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("destroy")) { Destroy(gameObject); } else if (collision.gameObject.CompareTag("player")) { Time.timeScale = 1f; // Stop the background movement if (backgroundScript != null) { backgroundScript.StopMovement(); } // Stop spawning EndlessSpawn endlessSpawnScript = Object.FindFirstObjectByType<EndlessSpawn>(); // Updated method if (endlessSpawnScript != null) { endlessSpawnScript.StopSpawning(); } // Start a coroutine to destroy the player after a 3-second delay StartCoroutine(DestroyPlayerAfterDelay(0.8f)); } } IEnumerator DestroyPlayerAfterDelay(float delay) { yield return new WaitForSeconds(delay); if (player != null) // Ensure the player exists before trying to destroy { Destroy(player); Time.timeScale = 1f; } }}
I hope this gives you a good idea of the problem, but please let me know if you need any more information!
Thanks in advance for your help!
