#Invoke
1 messages ยท Page 1 of 1 (latest)
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
I'm pretty sure that code above doesn't work? ๐ค
In the documentation for Invoke()
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
void Start()
{
Invoke("LaunchProjectile", 2.0f);
}
void LaunchProjectile()
{
// Do stuff
}
Indeed, you've made a weird change.
What you have done since last time is move the code from inside OnCollisionEnter2D
into FixedUpdate()
I will explain first how to simply make this work.
And then why what you did with FixedUpdate was wrong.
Okay, that sounds good
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
Correct
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
And change "Player" to "JumpPad" ? If I give the JumpPad a JumpPad tag, ofcourse
Yes, exactly.
I see now, and if I wanted to delay this, I should Invoke the Bounce_Effect, to my understanding?
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
}
But if I change private void Bounce_Effect() to private void Invoke(), the code doesnt make sense anymore
Post what you changed.
I am actually not sure what to change
Doesn't matter. Show me what you did change. I'll explain why that doesn't make sense.
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?
This is creating Bounce_Effect:
private void Bounce_Effect()
{
rb.AddForce(Vector2.up * bounce, ForceMode2D.Impulse);
}
it stays as it is
Yes
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(...);
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 :}
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?
You're welcome ๐
In retrospect, you might now find the documentation's script example to be easy to read.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
It certainly helped, and im sure it will help me understanding future scripts more
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.
Bookmarked ๐๐พ
The server has a lot of competent helpers. You'll be good ๐
Have a nice day.
Thank you again, have agreat day as well! 