※こちらのチュートリアルは古いチュートリアルとなっています。最新版は「PlayCanvasの基本操作編2020」からご確認ください!
「敵を作る」
弾をぶつける敵を作ります。
HIERARCHYからRootを選択して、ASSETS内のmodels -> UFOフォルダ内のPH_ufo.jsonをSCENEにドラックアンドドロップします。
INSPECTORを操作して以下のように設定します。
Entity
Position:[-10, 0, 10]
Scale:[0.5, 0.5, 0.5]
<ADD COMPONENT>
*Collision
Half Extents:[1, 0.5, 1]
*Rigid Body
Type:[Kinematic]
*Scripts
[+ADD Script]
-> +New Script
enemy.js
enemy.jsをCode Editorから開き、以下のように記述します
var Enemy = pc.createScript('enemy');
// initialize code called once per entity
Enemy.prototype.initialize = function() {
//衝突判定のイベントを設定
this.entity.collision.on("collisionstart", this.death, this);
};
// update code called every frame
Enemy.prototype.update = function(dt) {
//回転する力を加える
this.entity.rigidbody.angularVelocity = new pc.Vec3(0,50,0);
};
// swap method called for script hot-reloading
// inherit your script state here
Enemy.prototype.swap = function(old) {
};
Enemy.prototype.death = function(result){
if(result &&
result.other.rigidbody &&
result.other.name === "clone"){
//衝突したコリジョンを持った相手の名前が"clone"だったら
this.entity.destroy();//自分自身をdestroy
result.other.destroy();//衝突した相手をdestroy
}
};
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/
最初の状態だと、bulletは剛性を持っていないので、collisionとrigidbodyを追加しINSPECTORを操作して以下のように設定します。
Collision
Type:[Sphere]
Radius:[0.25]
Ridid body
Type:[Kinematic]
実行して、弾が衝突するとUFOが消えることが確認できたらOKです。
一体じゃつまらないのでHIERARCHYのDuplicate Entityから複製して、たくさん配置してみましょう
チュートリアル - PlayCanvasの基本操作 -
チュートリアル - PlayCanvasの基本操作 - 6/7
コメント
0件のコメント
サインインしてコメントを残してください。