Search code examples
c#unity-game-engineparticle-system

Adding force to objects that interact with a particle system C#


I currently have a particle system that is suppposed to represent a leafblower. I have got this working to proc on and off on mouse clicks.

The thing is, I want it to "blow" objects out of the way like a realistic leafblower. I have heard some people like to use AddForceAtPosition or something like that, it's just I do not know how to use it.

Currently I am enabling and disabling a box collider when my 'leafblower' is on and having everything the collider touch be knockedback but this proves to have various in game problems.

Here is my code I am working with:

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()
{

}

}

What force should I be adding or what should I put in a void OnCollisionEnter()? Thank you so much :)


Solution

  • What you probably need is Particle System collision OnParticleCollision. Please check Unity Scripting API here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnParticleCollision.html