Week 2: Collision detection


In video games a collision happens when two objects intersect, or when the distance between the objects falls below a small tolerance.

Today you are going to edit the player script so that it can detect when it collides with objects in your level. We’ll start by making deadly obstacles that restart your game.

Week 2 programming concepts:

  • Selection with conditional logic
  • If-statements

1: Placing objects as obstacles

  • Make sure regular layer is selected (show conversion to tile by accident)
  • Place a few obstacle objects

  • Remember you can drag objects around or remove them
  • In the next step we’ll create a script for the obstacles

Place some objects in your the level that the player character can collide with. Watch this video for a refresher on placing objects in your level.

 

2: Scripting your obstacle

  • Right-click on one of the obstacles and add script
  • “When created” from events
  • “Add tag tag name on myself” from sensing
  • Change tag name to “obstacle”

  • Close the script editor and save this as obstacle
  • In the next step we’ll add this script to all our obstacles

Now let’s add scripts the objects so that they can be detected by your collision script later. Watch the video below to create a script that adds an identification tag to an object.

 

3: Adding scripts to other objects

  • Open the scripts sidebar
  • Select the obstacle script
  • Click on obstacle objects to attach the script

You can add your script to any object in your level that you want to be collidable. Watch the video below to learn how to add a script to multiple objects.

 

4: Detecting the collision

  • Right-click on player and edit script
  • “When touched get toucher” from physics (explain when touched and the toucher)
  • “If do” from control flow
  • “Myself has tag” from sensing
  • Replace “myself” with “instance toucher” from variables
  • Change “tag name” to “obstacle”
  • “Go to next level” from control flow
  • Change next to current

  • Play the game and collide with obstacle
  • In the next step we’ll add a background image

Now we’ll add some code to the player to do something when it hits an object. You’ll code the ‘something’ in the next step. Watch the video below to learn how to detect collisions.

 

5: Coding a non-collidable background

  • Make new layer and drag to bottom
  • Place background image in this layer

  • Add script
  • “When created” from events
  • “Set physics enabled true” from physics, change to false
  • “Constantly” from events
  • “Set x position of myself to 0” from transform
  • “Camera x” from looks
  • Duplicate “set x position” and change x to y

  • Close and save script
  • Play game to test
  • Next week we’ll add win and lose screens

Sometimes you don’t want the player to collide with an object. Background images are a good example; in this step you’ll code the background to be non-collidable. Watch the video below to learn how to make an object follow the camera and disable its physics.

 

results matching ""

    No results matching ""