Post by jgiroux on Aug 19, 2016 3:59:35 GMT
Follow the original thread down to the bottom.
Trigger Particle on Collider Add-On <-- Click Me
Damage Particle Trigger Effect
requested by vike1984
add a collider component to whatever it is that is causing the damage.
set collider component to "Trigger"
edit the boundries of the collider to your liking (just remember our player has to enter this trigger so don't make it so small that a player cannot effectively *enter* it)
Create a new c# script, call it "DamageCollider" (without quotes) . Copy/paste the below code into it, save and compile/build.
using UnityEngine;
using System.Collections;
using Invector.CharacterController;
using Invector;
public class DamageCollider : MonoBehaviour
{
public GameObject ParticlePrefab; // particle prefab we want to create
public Vector3 ParticleOffset = new Vector3(-1f, 1f, 0f); // offset for particle, leave as is for it to show at players head level
private bool DoOnce = false; // a bool to keep trigger from repeating more than once
public bool CauseDamage = false; // check true if we want damage applied as well/ leave false if no damage is to be taken
public int DamageAmount; // If above true, how much damage is taken.
void OnTriggerEnter(Collider other)
{
// check if we are our player,
if (other.gameObject.tag == "Player")
{
if (DoOnce == false) // hasnt triggered yet
{
PlayParticleAtLocation(); // Play Particle
if (CauseDamage == true)// if we want damage to occur
{
other.GetComponent<vThirdPersonController>().currentHealth = other.GetComponent<vThirdPersonController>().currentHealth + -DamageAmount; // adjust players health for damage
}
DoOnce = true; // lock trigger to not happen again till exit collider
}
}
}
void OnTriggerExit(Collider other)
{
// check if we are our player
if (other.gameObject.tag == "Player")
{
if (DoOnce == true)
{
DoOnce = false; // reset bool to false so we can retrigger event again
}
}
}
public void PlayParticleAtLocation() // custom function for instantiating prefab
{
Instantiate(ParticlePrefab, transform.position + ParticleOffset, Quaternion.identity);
// Add other code here, if you want to add to it, sound perhaps, etc...
}
}
Add this script now to your object with the collider on it (that was set to Trigger)
notes:
You must have a particle prefab setup beforehand (this is beyond the scope of this quick script snippet)
steps:
1) add particle prefab to "Particle Prefab" in Game Inspector
2) Particle Offset (allows you to manually tweak where the VFX particle will be played upon the player (currently its set to go off near players head)
3) *IF* you want damage to happen, make sure "Cause Damage" is checked.
3b) *IF* you have Cause Damage on, put *HOW* much damage will be taken.
Let me know if you have any questions, and Thank you for supporting Invectors ThirdPerson Controller.
lastly: This script will effectively instantiate *any* game object, doesnt necessarily have to be a particle FX, could be any game object. Just FYI.