Forum begins after the advertisement:


[Part 10] Boss hit very fast and Null ref

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 10] Boss hit very fast and Null ref

Viewing 12 posts - 16 through 27 (of 27 total)
  • Author
    Posts
  • #16578
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    Yes. The boss still making the pillar stack into 1 place only. I has tried some different way to fix it but still not work at all

    #16581
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Sorry Pepe. Yes, please share your project with us.

    #16586
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    here is the link of my project. Sorry for replying late
    https://drive.google.com/file/d/1LWgEOmlEMzlzOTJFGvjGJZZn2IfaXOVH/view?usp=drive_link

    #16595
    A_DONUT
    Level 6
    Moderator
    Helpful?
    Up
    1
    ::

    I’d be glad to help you with the boss attack issue in your code. Here’s a breakdown of the problems and potential solutions, incorporating insights from the ratings:
    Issues:

    • Rapid Punches: The TriplePunch coroutine triggers the “Punch” animation three times in rapid succession using WaitForSeconds delays. This creates the illusion of very fast punches.

    • Null Reference Error: The PunchDamagePlayer function uses CorruptedMonk.Instance to access the monk’s properties, but it might be null if the CorruptedMonk object hasn’t been instantiated yet.

    • Animator Reset: The animation triggers are reset after each punch using anim.ResetTrigger(“Punch”), which might prevent the animation from playing smoothly if you intend for it to flow continuously during the triple punch sequence.
      Solutions:

    • Control Punch Speed: To slow down the punches, modify the delays within the TriplePunch coroutine. Instead of WaitForSeconds, consider using yield return new WaitUntil(() => // condition to wait for). Here’s an example:

       IEnumerator TriplePunch()
      {
       attacking = true;
       rb.velocity = Vector2.zero;
      

      // Wait until a certain animation frame is reached (adjust frame number as needed) yield return new WaitUntil(() => anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0.5f);

      anim.SetTrigger("Punch");

      // Wait for a set duration (adjust as needed) yield return new WaitForSeconds(0.3f);

      // Repeat for the second and third punches with appropriate delays // ...

      ResetAllAttack(); }

    • Handle Null Reference: Ensure that the CorruptedMonk object exists before accessing it. You can add a check or use a safer reference approach:

      • Check for Existence:

         void PunchDamagePlayer()
        {
        if (CorruptedMonk.Instance != null)
        {
           // Access CorruptedMonk properties here
           Hit(CorruptedMonk.Instance.sideAttackTransform, CorruptedMonk.Instance.sideAttackArea);
        }
        } 
        
      • Safer Reference (if CorruptedMonk is a Singleton):

         private CorruptedMonk corruptedMonk;
        void Start()
        {
        corruptedMonk = CorruptedMonk.Instance;
        }
        
     void PunchDamagePlayer()
    {
        if (corruptedMonk != null)
        {
            Hit(corruptedMonk.sideAttackTransform, corruptedMonk.sideAttackArea);
        }
    }
    
    • Consider Animation Flow: If you want the punches to blend smoothly, keep the animation triggers active (avoid ResetTrigger) and adjust delays to control the flow.
      Additional Considerations:
    • Debugging: Use Debug.Log statements or the debugger to inspect values and identify where errors occur.
    • Animation Speed: The animation speed can be controlled within the animation editor if the code doesn't directly set it.
    • State Management: If Monk_State1 triggers other behaviors, ensure they don't interfere with the punch timing.
      By combining these solutions and refining your code, you should be able to achieve the desired punch speed, avoid null reference errors, and potentially create a smoother animation flow.
    has upvoted this post.
    #16596
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    I forgot about this. I had already handle the rapid punch. It because in the animation. I create 3 event for it with out knowing that i was make it 3. So i deleted it and the boss in no longer making ultra fast punch. The problem is only the null ref and the water pillar at 1 place. And the death sceen + resume in other topic. Glad if you could help me find out the way to fix the deathsceen and the resume issues. My project link in the above. You could check it out

    #16597
    A_DONUT
    Level 6
    Moderator
    Helpful?
    Up
    0
    ::

    I’d be glad to help you with the null reference and water pillar issues in your project.
    Null Reference Error:
    To address the null reference error, you’ll need to ensure that the CorruptedMonk.Instance is not null before accessing its properties. Here are a few ways to do this:

    • Check for Null:
      • Before accessing any properties of CorruptedMonk.Instance, check if it’s null:
        if (CorruptedMonk.Instance != null)
        {
        // Access properties here
        Hit(CorruptedMonk.Instance.sideAttackTransform, CorruptedMonk.Instance.sideAttackArea);
        }

    Safer Reference (Singleton Pattern): If CorruptedMonk is a Singleton, you can store a reference to it in a variable:

    private CorruptedMonk corruptedMonk;
    
    void Start()
    {
        corruptedMonk = CorruptedMonk.Instance;
    }
    
    void PunchDamagePlayer()
    {
        if (corruptedMonk != null)
        {
            Hit(corruptedMonk.sideAttackTransform, corruptedMonk.sideAttackArea);
        }
    }

    Water Pillar at One Place:
    To make the water pillar appear at different locations, you’ll need to modify the code that determines its spawn position. Here are a few approaches:

    • Random Position:

      • Generate a random position within a specified range:
        Vector3 randomPosition = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), 0);
        Instantiate(waterPillarPrefab, randomPosition, Quaternion.identity);
    • Predefined Positions:

      • Create an array of predefined positions and choose one randomly:
        Vector3[] positions = { position1, position2, position3 };
        int randomIndex = Random.Range(0, positions.Length);
        Instantiate(waterPillarPrefab, positions[randomIndex], Quaternion.identity); 
    • Pattern-Based Placement:

      • Implement a pattern (e.g., circular, linear) to determine the spawn positions.
        Additional Tips:
    • Debugging: Use Debug.Log statements to print variable values and track the execution flow.

    • Error Handling: Implement error handling mechanisms to catch potential exceptions and provide informative messages.

    • Testing: Thoroughly test your code to identify and fix issues early on.

    • Code Organization: Keep your code well-organized and readable.

    • Comments: Add comments to explain the purpose of different code sections.
      By following these suggestions and carefully reviewing your code, you should be able to resolve the null reference and water pillar issues.
      Please let me know if you have any other questions or require further assistance.

    #16609
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    1
    ::

    pepecry, for your resume issue, you need to assign the Game Manager in the Scene, not the Prefab in the Project window.

    View post on imgur.com

    has upvoted this post.
    #16610
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    I can’t get the boss to spawn the water pillars in your project though. Any suggestions?

    #16611
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    You just need to delete or bring the spawn water pillar from phase 2 to phase 1 and replace it with sweepkich. I believe it will work. And tks you

    #16628
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Finally found what is causing your diving pillar issue, it is because your Animator for the water blast is controlling its position. Hence, even though your script is correct, the Animator always forces it to assume a fixed position beyond the first frame.

    Remove the position keyframes on your water pillar animation and it will fix the issue:

    View post on imgur.com

    As for the Death Screen, make sure the button action uses the Game Manager object in the Scene, not the Prefab.

    #16630
    pepecry
    Level 7
    Participant
    Helpful?
    Up
    0
    ::

    Tks you terence but I want to ask another question. When I using the desolate dive, my character go through the ground. Is that because my character fall to fast and the groundcheck doesn’t make it on time? what should I do?

    #16631
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Yes, it’s possible your character is going too fast. You can set his Rigidbody’s Collision Detection Mode to Continuous Dynamic to fix this.

    Read this to understand more about what this setting means: https://blog.terresquall.com/2019/12/collision-detection-modes-in-unitys-rigidbody-component/

Viewing 12 posts - 16 through 27 (of 27 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: