- Home /
The question is answered, right answer was accepted
2D Sidescroller enemy AI jump help! Picture Included!
Hello everyone. I'm learning Unity and this is my very first project.
I have a 2d side scroller with a player and an enemy AI.
The enemy AI targets the player and can follow the player and flip directions as needed.
my question is how can I get this AI to jump to the platform the player does in order to continue following the player?
I've read things about waypoints, navmesh, triggers, ect ect. The problem is I don't know what they all can do and like I said I'm very new so a tutorial would be amazing!
Answer by Geads · Aug 31, 2017 at 04:45 PM
How would you link both box colliders? I understand if the AI enters the first one then jump, but how would you script if ai is in bottom 2dcollider and player is in top 2dcollider then jump?
in your player code create a bool that if player is in up boxCollider2D then make the bool true. then in your enemy code create another bool when he's in down collider2D then the bool true.
then in your enemy code use something like this
public playerController player; // object that has your player codes
void Awake () {
player = FindObjectOfType<playerController> (); // so you can access to script of the object that has playerController script.
}
void Update(){
if (player.IsinTheUpCollder2D && enemyIsIntheGroundCollider2D) {
// IsinTheUpCollder2D it's box collider that is on the upper Ground //enemyIsIntheGroundCollider2D it's box collider that is on the Ground
jump;
}
}
hope you get the idea
Answer by PersianKiller · Aug 29, 2017 at 06:25 PM
dude I give you an easy solution :). place a box collider 2d on the ground. then if enemy is in the boxcollider that is on the ground and the player is in the boxcollider that is top of the ground,then enemy shold jump :).you can use OnTriggerEnter2D function and it's very easy .hope it gives you some ideas :D
Answer by Duvic · Aug 31, 2017 at 05:33 PM
Its a fine idea to do 2dTriggers and its not wrong but i would use a ray cast. it's not that compleceted to lern and it is so importent for unity i use it in one way or anather in evre game i make. you culd chack the distance with the ray and tell him to jump at a certain point, this way you dont need to manually position every jump point.
heres the link for 2d ray casting: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
hehehe, yes ,I heard that we should use raycast , but I prefer to do my works with simplest way :)
Follow this Question
Related Questions
2D jumping raycast question 0 Answers
Problem with rotated colliders 0 Answers
2D Platformer Jump while Running Android 1 Answer