Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question 2D Animation over damage zones in mini-RPG

Discussion in 'Animation' started by Frostysh, Aug 3, 2022.

  1. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    Trying to finish this tutorial: Visual Styling - Particles - Unity Learn, and this is my code for script program of main character and screenshot of the issue...

    I am registering public ParticleSystem Get_Hit and then executing it through Instantiate() program in the 83th line of the scripting text.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Idiotic_Ruby_Controller : MonoBehaviour
    6. {
    7.     public int maxHealth = 5;
    8.  
    9.     public float time_invincible = 2.0f;
    10.  
    11.     public float maxSpeed = 3.0f;
    12.  
    13.     public int health { get { return currentHealth; } }
    14.  
    15.     public GameObject projectile_prefab;
    16.  
    17.     public ParticleSystem Get_Hit;
    18.  
    19.     int currentHealth;
    20.  
    21.     bool is_invincible;
    22.     float invincible_timer;
    23.  
    24.     Rigidbody2D rigidbody2d;
    25.     float horizontal;
    26.     float vertical;
    27.  
    28.     Animator animator;
    29.     Vector2 look_direction = new Vector2(1, 0);
    30.  
    31.  
    32.     // Start is called before the first frame update.
    33.     void Start()
    34.     {
    35.         animator = GetComponent<Animator>();
    36.         rigidbody2d = GetComponent<Rigidbody2D>();
    37.         currentHealth = maxHealth;
    38.     }
    39.  
    40.     // Update is called once per frame.
    41.     void Update()
    42.     {
    43.         horizontal = Input.GetAxis("Horizontal");
    44.         vertical = Input.GetAxis("Vertical");
    45.         Vector2 move = new Vector2(horizontal, vertical);
    46.      
    47.         if(!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    48.         {
    49.             look_direction.Set(move.x, move.y);
    50.             look_direction.Normalize();
    51.         }
    52.      
    53.         animator.SetFloat("Look X", look_direction.x);
    54.         animator.SetFloat("Look Y", look_direction.y);
    55.         animator.SetFloat("Speed", move.magnitude);
    56.      
    57.         if (is_invincible)
    58.         {
    59.             invincible_timer -= Time.deltaTime;
    60.             if (invincible_timer < 0)
    61.                 is_invincible = false;
    62.         }
    63.      
    64.         if(Input.GetKeyDown(KeyCode.C))
    65.         {
    66.             Launch();
    67.         }
    68.     }
    69.  
    70.     void FixedUpdate()
    71.     {
    72.         Vector2 position = rigidbody2d.position;
    73.         position.x = position.x + maxSpeed*horizontal*Time.deltaTime;
    74.         position.y = position.y + maxSpeed*vertical*Time.deltaTime;
    75.         rigidbody2d.MovePosition(position);
    76.     }
    77.  
    78.     public void ChangeHealth(int amount)
    79.     {
    80.         if (amount < 0)
    81.         {
    82.             animator.SetTrigger("Hit");
    83.             Instantiate(Get_Hit, rigidbody2d.position, Quaternion.identity);
    84.          
    85.             if (is_invincible)
    86.                 return;
    87.  
    88.             is_invincible = true;
    89.             invincible_timer = time_invincible;
    90.         }
    91.             currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    92.             Debug.Log(currentHealth + "/" + maxHealth);
    93.     }
    94.  
    95.     void Launch()
    96.     {
    97.         GameObject projectile_object = Instantiate(projectile_prefab, rigidbody2d.position + Vector2.up*0.5f, Quaternion.identity);
    98.         Projectile projectile = projectile_object.GetComponent<Projectile>();
    99.         projectile.Launch(look_direction, 300);
    100.      
    101.         animator.SetTrigger("Launch");
    102.     }
    103. }

    This is the screenshot of character hit damage zone, and as we can see in the Hierarchy created insane amount of Get_Hit game object without no reason.

    And this is how it suppose to be, exception animation is behind main character and must be above.

    Particle system of Get_Hit main module



    1) How to make damage zone show particle system not continuously? But rather discreate per time.

    2) How to make animation appear above the character?

    3) What kind of nonsense is that? Well it seems no affection the game process, but still somekind of insane massage.
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. UnityEditor.Graphs.Edge.WakeUp () (at <00d3e448360b44c796142175be875975>:0)
    3. UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List`1[T] inEdges, System.Collections.Generic.List`1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <00d3e448360b44c796142175be875975>:0)
    4. UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <00d3e448360b44c796142175be875975>:0)
    5. UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <00d3e448360b44c796142175be875975>:0)
    6. UnityEditor.Graphs.Graph.WakeUp () (at <00d3e448360b44c796142175be875975>:0)
    7. UnityEditor.Graphs.Graph.OnEnable () (at <00d3e448360b44c796142175be875975>:0)

    P. S. My gratitude for the future answers.
     
  2. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    Somehow the problem number three is self-fixed, I don't know how it is happened, but after restart of "Unity Editor" the problem is disappeared. The problem number two is fixed by mineself too. But the number one is still persist.