You are on page 1of 16

MMNMTCO

Week 4: Interaction

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Objectives
● Kumustahan
● Templates
● Mouse button input
● Shooting: paintball style
● Activity 3

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Kumustahan!
● How are you this week?
● Any questions about our topic?

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Note:
● You can control the shininess of your objects
by setting the color of the specularity on the
material.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Templates
Templates are "saved" entities that
can store all the properties and
components that you've previously
assigned to them.
Templates are displayed with a
diamond icon in the Assets panel
For example, you can have an Enemy
template that already includes its
rigidbody and collision so you can
just spawn multiple copies of it.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Templates

A templated entity can contain children too.


In Unity, these are called prefabs.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Templates
Create a template by right clicking
on an entity's name in the
Hierarchy panel. Select Template >
+ New Template.

A new template will be created in


the currently open folder in the
Assets panel.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Mouse button input

Keyboard Mouse

if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) if (this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT))

if (this.app.keyboard.isPressed(pc.KEY_SPACE)) if (this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT))

if (this.app.keyboard.wasReleased(pc.KEY_SPACE)) if (this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT))

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Shooting: Paintball style
The idea:
1. Make a "bullet" template

2. Spawn this bullet template in front of


the gun muzzle whenever the gun fires

3. Apply a lot of force on the bullet to make


it go forward

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


The gun
You can grab something from the asset store, or just use a simple placeholder
for now.

Add an entity and place it inside the gun, right in front of where the muzzle
should be. Name this entity "bulletEmitter".

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Bullet template
1. Create a sphere. Scale it down to about 0.05%.
2. Add a sphere collision and set its radius to match.
3. Add a rigidbody and set it to Dynamic. Leave the mass at 1.
4. In the hierarchy panel, right click on the sphere's name and turn it into a template.
5. After you create the template, you can delete the bullet that's in the scene.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Coding the behavior
1. Select the gun and create a new script for it. Call this shootBullet.
2. Let's add an attribute to the code. This allows us to assign an object that
the gun will use as a reference point. The gun will spawn bullets on this
object.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Coding the behavior
3. In PlayCanvas, parse the shootBullet script.

4. This will add the bulletEmitter slot to the


interface.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Coding the behavior
5. Drag the bulletEmitter entity from the
hierarchy into this slot.

6. You can now refer to this entity in your


code by using this.bulletEmitter.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Coding the behavior
7. Add the following code to the
shootBullet script under the
update function:

8. You'll need to change the ID


in line 5 to match your bullet
template's ID.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.


Activity 3
● Make your player shoot bullets. We're using
projectiles for now, but we'll learn an alternate
way (hitscan) of treating shooting next time.

● The input should be bound to either the


spacebar or the left mouse button.

● Submission: if you've already added me as an


admin, just pass the project link like before.

● If you created a new project, add me (cruzjf)


Use the Publish/Download
as an admin, then pass the project link. link on the left menu bar,
then add cruzjf under
Team > Project Members.

Copyright © 2022 De La Salle - College of Saint Benilde. All rights reserved.

You might also like