#+HTML_HEAD: #+HTML_HEAD: #+title: Lesson 12: Player-Enemy Collisions * Links - [[../../arpg/toc.org][< Back to Main TOC]] - [[https://youtu.be/WVQkOWY3zxQ?si=UILX6fBQ_Zw1cZoK][Player-enemy collision]] * Notes ** add collision shape to enemy sprite - add CollisionShape2D to sprite root *** add shape - select collision shape 2D node from Scene Panel - Go to Inspector->CollisionShape2D->Shape - select 'CapsuleShape2D' from dropdown *** modify shape - Inspector->CollisionShape2D->Node2D->Transform - Go to Rotation and set to 90 degrees - Modify shape via handles to adjust it over sprite *** set the z-index - in CollisionShape2D to 0 - in sprite root node to 1 *** make sure collision shape fits all the animation frames - go to the AnimateSprit2D node - Inspector->AnimatedSprite2D->Animation->Frame - scroll through the frames to see if they fit the collision shape properly ** modify the player script to handle the collision *** default collision code - default collision handling occurs between two CharacterBody2D nodes - limits each others movements - occurs after the =move_and_slide= function is called #+begin_src godot # Using move_and_collide. var collision = move_and_collide(velocity * delta) if collision: print("I collided with ", collision.get_collider().name) #+end_src *** handle collisions (scaffolding) - first get the number of collisions that occurred - this is in =get_slide_collision_count()= function - loop through the number with an index - in each loop get the /collision info object/ for that index with =get_slide_collisions()= - =get_slide_collision= takes an index - to get the /collider/ call =get_collider()= on the /collision info object/ - the function scaffolding: #+begin_src godot # this is the basic function func handleCollision(): for i in get_slide_collision_count(): var collision = get_slide_collision(i) var collider = collision.get_collider() priint_debug(collider.name) #+end_src - place this function in =func _physics_process(delta)= - put it after =move_and_slide= but before =updateAnimation= - example: #+begin_src godot func _physics_process(delta): handleInput() move_and_slide() handleCollision() updateAnimation() #+end_src * Video Contents [[https://www.youtube.com/watch?v=WVQkOWY3zxQ][Lesson Link]] #+attr_html: :class content - [[https://www.youtube.com/watch?v=WVQkOWY3zxQ][00:00 Introduction]] - [[https://youtu.be/WVQkOWY3zxQ?si=vhyjv3xTfYhvAMmE&t=28][00:28 body-body collision]] - [[https://youtu.be/WVQkOWY3zxQ?si=qLZquUVuAC-yYN1t&t=238][03:57 layer and mask]] - [[https://youtu.be/WVQkOWY3zxQ?si=GqVUob9wQvm1YH_9&t=298][04:58 hurt boxy and hit box]] - [[https://youtu.be/WVQkOWY3zxQ?si=rhqR5crCcWh7vgSH&t=394][06:34 hit box animation]] - [[https://youtu.be/WVQkOWY3zxQ?si=DIBeG4TRZdhcds0g&t=711][11:51 fixing move animation]] - [[https://youtu.be/WVQkOWY3zxQ?si=CTRznDX_bRokCO0d&t=751][12:31 hurt-hit box collisions]] - [[https://youtu.be/WVQkOWY3zxQ?si=fUUy5KhrAFLzbWdA&t=827][13:47 outro]]