#Invoke

1 messages ยท Page 1 of 1 (latest)

silk stone
#

All the more reason to help you understand this faster

inland nest
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JumpPad : MonoBehaviour
{
   
    private float bounce = 12f;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Invoke("bounce", 2);
    }
    void FixedUpdate()
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            collision.gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * bounce, ForceMode2D.Impulse);
        }
    }

    
}
#

Oh hey, it works, awesome

silk stone
#

I'm pretty sure that code above doesn't work? ๐Ÿค”

inland nest
#

It indeed doesnt

#

The name 'collision' doesnt exist in current context

silk stone
silk stone
inland nest
#

Okay, that sounds good

silk stone
#

Now, read my example of Invoke

#

The example uses Start() to run Invoke
Start() runs automatically when an object has been enabled for the first time in a scene

#

Invoke takes the name of a method in its parameter.
If either the parameter name or the method name do not match, the code will return an error.

#

A better way of writing it would be

Invoke(nameof(LaunchProjectile), 2.0f)
#

Anyway.
You don't want to execute the jump effect via Start
You want it to execute 2 seconds after one collider has touched another

inland nest
#

Correct

silk stone
#
public class JumpPad : MonoBehaviour
{
    private float bounce = 12f;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            Rigidbody2D rb = collision.gameObject.GetComponent<Rigidbody2D>();
            Bounce_Effect(rb);
        }
    }

    private void Bounce_Effect(Rigidbody2D rb)
    {
        rb.AddForce(Vector2.up * bounce, ForceMode2D.Impulse);
    }
}
#

This will make it happen instantly, without delay

#

Since we need a reference to the Player's Rigidbody, it would make more sense to have this script on the player, rather than each JumpPad in the game.

#

In order to modify this one (same approach)

#

you'll need to store a private reference to the rigidbody, because we can't pass parameters with Invoke

inland nest
inland nest
#

I see now, and if I wanted to delay this, I should Invoke the Bounce_Effect, to my understanding?

silk stone
#

Yes, but there is one problem: We can't pass values when using invoke

#

Therefore

#
public class JumpPad : MonoBehaviour
{
    private float bounce = 12f;
    private Rigidbody2D rb;  // <--- Added this
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            rb = collision.gameObject.GetComponent<Rigidbody2D>();
            Bounce_Effect();
        }
    }

    private void Bounce_Effect()
    {
        rb.AddForce(Vector2.up * bounce, ForceMode2D.Impulse);
    }
}
#

And I'll leave replacing Bounce_Effect() with Invoke to you :)

#

That code is without changing Player/JumpPad

#

To explain what was changed:

private int o;  // This is a variable field, accessible to only this instance of the script
private void Some_Method()
{
    int i = 0;  // This is a local variable, only accessible from inside the parent method
}
inland nest
#

But if I change private void Bounce_Effect() to private void Invoke(), the code doesnt make sense anymore

silk stone
#

Post what you changed.

inland nest
#

I am actually not sure what to change

silk stone
#

Doesn't matter. Show me what you did change. I'll explain why that doesn't make sense.

inland nest
#

In non-coding language, you 'create' Bounce_Effect in private void oncollisionenter2d, and then you say what Bounce_Effect means in private void Bounce_Effect, correct?

silk stone
#

This is creating Bounce_Effect:

    private void Bounce_Effect()
    {
        rb.AddForce(Vector2.up * bounce, ForceMode2D.Impulse);
    }
#

it stays as it is

inland nest
#

Yes

silk stone
#

Inside the collision call, we are calling Bounce_Effect() to be run, if the conditions are met

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            rb = collision.gameObject.GetComponent<Rigidbody2D>();
            Bounce_Effect();
        }
    }
#

This calls the method instantly.

Bounce_Effect();
#

Invoke can run any method, by inputting its name, and can have a delay.

#

So you remove Bounce_Effect(); and replace it with Invoke(...);

inland nest
#

I think that was my biggest issue; understanding that you have to reference Invoke right after the conditions are met (also lack of proper coding knowledge).
It works like charm

#

I cannot thank you enough for helping me today, you are a lifesaver and a very kind person, I wish there were more of you :}

silk stone
#

It's just a ridiculous amount of logic to absorb, and mindfucks to correct, when starting out.
Hope this helps build some starting point for you.

#

Any questions?

silk stone
inland nest
inland nest
# silk stone Any questions?

Right now, I dont have any, but if I find any issues later on and Google cannot help me, I will certainly go to the Discord for help again.

inland nest
silk stone
inland nest