首页 > 解决方案 > 向与粒子系统 C# 交互的对象添加力

问题描述

我目前有一个粒子系统,应该代表吹叶机。我已经让这个工作在鼠标点击时开启和关闭。

问题是,我希望它像逼真的吹叶机一样“吹”出物体。我听说有些人喜欢使用 AddForceAtPosition 或类似的东西,只是我不知道如何使用它。

目前,当我的“吹叶机”打开时,我正在启用和禁用一个盒子对撞机,并且让对撞机触摸的所有东西都被击退,但这证明在游戏中存在各种问题。

这是我正在使用的代码:

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

public class leafblower : MonoBehaviour {

private Rigidbody rb;
protected bool letPlay;
public ParticleSystem blow;
public Collider lcol;

// Use this for initialization
void Start () 
{
    rb = GetComponent<Rigidbody>();
    blow = GetComponent<ParticleSystem>();
    lcol.enabled = !lcol.enabled;
}

void LeafBlower()
{
    if (Input.GetKeyDown(KeyCode.Mouse1))
    {
        var isBlowing = blow.emission;
        isBlowing.enabled = true;
        lcol.enabled = !lcol.enabled;
    }
    else if (Input.GetKeyUp(KeyCode.Mouse1))
        {
            var isBlowing = blow.emission;
            isBlowing.enabled = false;
        lcol.enabled = !lcol.enabled;
    }

}
// Update is called once per frame
void Update()
{
    LeafBlower();
}
void FixedUpdate()
{

}

}

我应该添加什么力量,或者我应该在 void OnCollisionEnter() 中添加什么?太感谢了 :)

标签: c#unity3dparticle-system

解决方案


您可能需要的是粒子系统碰撞OnParticleCollision。请在此处查看 Unity 脚本 API:https ://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html


推荐阅读