How can i make an AI jump in UNITY 2D Platform Game,Unity 2 AI chase Jumping
Hello guys. I am new to this think with Unity and have 0 skills in scripting. It will be much of a help if you manage to answer my question.
I build i game where i have 2 characters: Player and Enemy. I used Astar Pathfinder to make the Enemy chase the player around the level. However the Enemy should be grounded object that has to walk and jump over platforms i build . Once the Enemy hit shoot or catch the player the game should end. Anyways, the Enemy initially is chasing the player and everything looks fine, but when it comes to jumping on the platform i can not figure out how to do it and where to find an answer for this. Could please help me with this. I want make the Enemy to jump on platform while it chases the Player. I would appreciate any help i could get resolving this problem i have. Thank you very much
Answer by SilverFang180882 · Aug 05, 2020 at 02:04 PM
Stumbled across this while looking for a solution to make my AI double jump (my coroutine waits for him to land before doing the second jump).
Anyway, here's what I have to make an AI enemy jump (assuming you already have him moving left or right, based on which side the player is in relation to him);
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
"jumpHeight" is a float value you'd create yourself. Whatever value you set it to will depend based on how high you want your character to jump.
Hope that helps!
Where would I put the code you have provided? I am extremely new to Unity and writing code for it and wondered if this goes in the void update, void start, or somewhere else, and creating the float value is just outside on it's own correct? sorry if this is a lot of questions like I said I am very new to this but still want to make an enemy that jumps
I'm not sure what your code looks like, but do you have something which enables the enemy to detect the platform? And what he should do when he/she comes into contact with one? If you did, you'd put that jump code in there. Pretty much his/her "action" when he detects the platform you want him/her to jump on.
EDIT: I just realised you aren't the original poster (unless you're using two accounts). It's usually in Update, but Update runs continuously, so if you put it in there just like that, the enemy would just jump continuously, effectively flying off into the sky. You'd need to put it within whatever condition which you want to trigger the enemy's jump.
For example, if you want the enemy the jump at the exact same time as the player, it'd be something like this:
if (thePlayer.grounded == false)
{
GetComponent().velocity = new Vector2(GetComponent().velocity.x, jumpHeight);
}
This would go in Update, so the enemy will jump every time the player does. I personally like to create a separate function for the jump itself (or a coroutine, if I want to delay it).
If you're very new with code, I'd highly suggest finding a unity tutorial series and following through it. Will really help you learn with games coding.
Your answer
