かぴばらさんの覚書ブログ (nonkapibara 自分メモ)

かぴばらさんの覚書ブログ (nonkapibara 自分メモ)

Unity勉強中です。 AR、VR、エンターテイメント全般、ワクワクする事が大好き♪♪ O(≧∇≦)O イエイ!!

【Unity】木に衝突すると爆発のパーティクルを発生させ、プレイヤーを爆風で飛ばす

環境メモ

⭐️Mac OS Mojave バージョン10.14

⭐️Unity 2018.2.15f1

実際に動かした動画はこちら↓↓

https://twitter.com/nonnonkapibara/status/1089212339746877440

 

1.🌲🌲Tree)側に「Particle System」を追加する

f:id:nonkapibara:20190127024852p:plain



2.Shapeを設定する

f:id:nonkapibara:20190127024916p:plain



3.Rendererを設定する

f:id:nonkapibara:20190127024934p:plain



パーティクルの画像を設定する

4.Treeタグを設定する。

f:id:nonkapibara:20190127024950p:plain



RigdbodyIs Kinematic チェックON

ColliderIs TriggerチェックON

5.プレイヤー側、Character Controllerを設定する

f:id:nonkapibara:20190127025015p:plain



6.キャラクターにライトを設定する(Point Light

f:id:nonkapibara:20190127025045p:plain

 

🌲木(Tree)側のスクリプト🌲

TreeScript.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TreeScript : MonoBehaviour {
    private ParticleSystem particle;
    bool HitPlayFlag = false; //衝突有無(衝突イベント開始true)

    // Use this for initialization
    void Start()
    {
        //パーティクル
        particle = this.GetComponent<ParticleSystem>();
        particle.Stop();

    }

    // Update is called once per frame
    void Update () {
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Debug.Log("Tree OnTriggerEnter");
            //パーティクルを表示する
            particle.Play();
            // 衝突イベント開始 
            HitPlayFlag = true;
        }
    }
    void LateUpdate()
    {
        //このゲームオブジェクトのパーティクルが生存しているかどうか.
        if (HitPlayFlag && !particle.IsAlive())
        {
            // 衝突イベント終了 
            HitPlayFlag = false;
            Destroy(gameObject);

        }
    }
}

 

CaracterTry04Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CaracterTry04Script : MonoBehaviour
{
    [SerializeField] float HitFlaySpeed = 0.5f; // 飛ばされるスピード
    [SerializeField] float HitCycleTime = 0.7f; //飛ばされる時間(ゲーム中断時間)
    float suspendTime = 0.0f;
    private ParticleSystem particle;
    private Vector3 playerPosition;
    bool HitPlayFlag = false; //衝突有無(衝突イベント開始true)

    void Start()
    {
        suspendTime = HitCycleTime;
    }

    void Update()
    {
        if (HitPlayFlag && IsSuspend())
        {
            //衝突イベント開始で、ゲーム中断中の場合、プレイヤーを上に吹き飛ばす
            suspendTime -= Time.deltaTime;
            Vector3 pos = this.gameObject.transform.position;
            this.gameObject.transform.position = new Vector3(pos.x + 0.5f, pos.y + HitFlaySpeed, pos.z);
        }
        else
        {
            // 衝突イベント中
            if (HitPlayFlag)
            {
                // 衝突イベント終わり
                HitPlayFlag = false;
                // 衝突した場所まで戻る
                Vector3 pos = playerPosition;
                this.gameObject.transform.position = new Vector3(pos.x, pos.y, pos.z);
            }
        }


    }

    public bool IsSuspend()
    {
        return suspendTime > 0.0f;
    }

    /*
     * OnTriggerEnter
     * 木と衝突した時に発生するイベント
    */
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Tree")
        {
            // 現在位置を保存
            playerPosition = this.gameObject.transform.position;
            // 衝突イベント開始 
            HitPlayFlag = true;
            suspendTime = HitCycleTime;
        }
    }

}

 完成!!!!

f:id:nonkapibara:20190127030627p:plain

 

 

 

 

Assetsは

KTK Effect Sample Set

を使用しました。

f:id:nonkapibara:20190126013138p:plain