Related Posts Plugin for WordPress, Blogger...

4-18 Create A Magic Wand And Spawn Fireball

今天要來製作魔法杖!這是要用來給大Boss使用的,但如果大家還記得我們製作的Weapon System的話,就知道製作的武器不但能給Enemy使用,也能給Player使用唷!

當玩家打完大Boss以後,就會掉落一根魔法杖在地上,玩家可以撿拾起來並更換成魔法杖,使用Boss的武器應該都是每個RPG要有的橋段吧!如果對玩家撿拾武器並跟換的系統不熟悉的話,請先參考以前的文章唷:
4-3 Weapon Pickup Points
https://rpgcorecombat.blogspot.tw/2018/01/4-3-weapon-pickup-points.html

另外魔法杖的製作,也牽涉到之前做的弓箭武器,也可以先複習這篇文章:
4-15 Finishing The Weapon System

首先,我用網球的3D模型當作火球的基底,這顆網球模型在Unity Standard Asset裡面有提供唷。

接著,我將Unity提供的粒子系統範例(FlamesParticleEffect)拉進火球中,效果如下圖。

接著,我再拉一個爆炸效果的粒子系統(BigExplosionEffect)到火球中。

記得在BigExplosionEffect的設定中,要將Looping跟Play On Awake關掉唷!

接著來修改Projectile.cs的程式碼吧!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Core;
using RPG.Character;
 
namespace RPG.Weapons{
 public class Projectile : MonoBehaviour {
 
  [SerializeField] AudioClip shotSound;
  [SerializeField] AudioClip collisionSound;
  [SerializeField] GameObject explosionParticle;
 
  [Header("If No Particle")]
  [SerializeField] float destroyDelayTime = 0.1f;
 
  GameObject shooter;
  AudioSource audioSource;
  float damageCaused;
  const float PARTICLE_CLEAN_UP_DELAY = 0.5f;
 
  void Start(){
   audioSource = gameObject.AddComponent<audiosource>();
 
   if (shotSound != null) {
    audioSource.PlayOneShot (shotSound);
   }
  }
 
  public void SetShooter(GameObject shooter){
   this.shooter = shooter;
  }
 
  public void SetDamage(float damage){
   damageCaused = damage;
  }
 
  void OnCollisionEnter(Collision collision){
   int layerCollidedWith = collision.gameObject.layer;
   // 若被碰撞的物體所處的Layer跟shooter的Layer不同,才會觸發攻擊機制
   // 如敵人就不會攻擊到敵人
   if (shooter && layerCollidedWith != shooter.layer) {
    DamageIfDamageable (collision);
    PlayEffectAudio ();
    PlayParticleEffect ();
    if (explosionParticle == null) {
     Destroy(gameObject, destroyDelayTime);
    }
   }
  }
 
  private void DamageIfDamageable(Collision collision){
   // 取得HealthSystem
   HealthSystem healthSystem = collision.gameObject.GetComponent<healthsystem>();
   if (healthSystem) {
    healthSystem.TakeDamage (damageCaused);
   }
  }
 
  void PlayEffectAudio(){
   if (collisionSound != null) {
    // 使用PlayOnShot播放技能音效,可避免和其他技能音效重疊在一起
    audioSource.PlayOneShot (collisionSound);
   }
  }
 
  void PlayParticleEffect(){
   if (explosionParticle != null) {
    GameObject particlePrefab = Instantiate (explosionParticle,
                                transform.position,
                                Quaternion.identity,
                                transform);
    particlePrefab.GetComponent<particlesystem> ().Play ();
    StartCoroutine (DestroyGameObjectWhenFinished (particlePrefab));
   }
  }
 
  IEnumerator DestroyGameObjectWhenFinished(GameObject particlePrefab){
   while (particlePrefab.GetComponent<particlesystem> ().isPlaying) {
    yield return new WaitForSeconds (PARTICLE_CLEAN_UP_DELAY);
   }
   Destroy (gameObject);
   yield return new WaitForEndOfFrame ();
  }
 }
}
</particlesystem></particlesystem></healthsystem></audiosource>

修改完以後,將Projectile.cs拉進Fireball中,Shot Sound是發射時的音效,Collision Sound是火球撞到東西時的音效,請大家自己設定唷,Explosion Particle則設定BigExplosionEffect。另外,大家可以將Rigidbody的Use Gravity關掉,這樣火球就不會受到重力的影響直直往目標的方向飛。

以上完成火球的設定了,接著來製作一根魔杖吧。我在網路上找到了一個不錯的免費3D模型:https://free3d.com/3d-model/frost-staff-98873.html

這個素材沒有太多的裝飾,拉進場景後可以修改成自己喜歡的配色。由於我要發射的是火球,所以將頂端的水晶體從藍色改成紅色,法杖本體改成黑色,也稍微修改了大小讓水晶體更明顯。

 接著,建立魔法杖的Weapon Config跟Grip Position,這一步驟不懂的話請先將以前武器系統的文章都看完唷。

我的魔法杖設定如下,另外一提,我將Additional Damage設為0,是為了方便管理Enemy的攻擊力。因為我只想要設定Enemy的攻擊力,而不想要特地管理Enemy的武器的攻擊力。當然,如果要給玩家使用的話,就再複製一個Weapon Config並額外設定攻擊力即可。

然後,在Enemy的Weapon System上設定Current Weapon Config為魔法杖。

來執行遊戲試玩看看吧!哇!Boss發射火球了,效果好猛。

找到正確的發射時間來躲避火球吧!

好可怕!這樣有打Boss的感覺呢!

留言