关于unity 使用插件fungus时需要碰撞检测的例子

1、导入fungus插件

下载好资源包之后,直接将其拖拽到Assets下即可

导入成功的标志是出现Tools

 配置对话:Tools-Create-Flowchart

接着Flowchart-open Flowchart Window-npc1(你们的应该是NEW...)

先将Execute On Event设置为None

双击右中下方加号:Narrative-Say,即可编辑对话

2、场景说明

(1)3D简易场景

一个Panel,Capsule(作为Player) ,Cube(作为npc)

配置:player:Collider不勾选Is Trigger,Rigidbody

player的代码:

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

public class player : MonoBehaviour
{
    public float movespeed = 2f;

    private float hor, ver;

    void Update()
    {
        hor = Input.GetAxis("Horizontal");
        ver = Input.GetAxis("Vertical");
        Vector3 dir = new Vector3(-ver,0 ,hor );//具体按自己场景更改
        //前后移动
        transform.position += dir * Time.deltaTime * movespeed;

    }
}

npc配置:

记得挂载脚本的chatname是你对话的名称我的是npc1

npc代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fungus;
public class npc : MonoBehaviour

{
    public string chatname;
    private bool canchat = false;
    private void Update()
    {
        say();
    }

    void say()
    {
        if (canchat)
        {
            //对话
            Flowchart flowchart = GameObject.Find("Flowchart").GetComponent<Flowchart>();
            //对话是否存在
            if (flowchart.HasBlock(chatname))
            {
                flowchart.ExecuteBlock(chatname);
            }

        }

    }
    private void OnTriggerEnter(Collider other)
    {

        if (other.CompareTag("Player"))
        {
            canchat = true;
        }
        
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            canchat = false;
        }
    }
}


(2)2D场景,和3D大差不大

player

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

public class player : MonoBehaviour
{
    public float movespeed = 2f;

    private float hor, ver;

    void Update()
    {
        hor = Input.GetAxis("Horizontal");
        ver = Input.GetAxis("Vertical");
        Vector3 dir = new Vector3(hor, ver, 0);
        //前后移动
        transform.position += dir * Time.deltaTime * movespeed;
        
    }
}
 

npc1

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

public class npc : MonoBehaviour
{
    public string chatname;
    private bool canchat = false;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            canchat = true;
            StartDialogue();
        }
    }

    void OnTriggerExit2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            canchat = false;
        }
    }

    void StartDialogue()
    {
        if (canchat)
        {
            // 获取Flowchart组件来开始对话
            Flowchart flowchart = GameObject.Find("Flowchart").GetComponent<Flowchart>();

            // 检查对话块是否存在
            if (flowchart.HasBlock(chatname))
            {
                flowchart.ExecuteBlock(chatname);
            }
        }
    }
}
 

以上有借鉴的代码,请勿商用哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值