|
Post by vike1984 on Aug 7, 2016 6:25:21 GMT
hello , I am tring to create particules that trigger on collider with the player ... I am tring to make that when the main character hit some surfaces he then bleed. anyone know how could I do this ? thx
|
|
|
Post by jgiroux on Aug 8, 2016 0:26:52 GMT
Hello, and welcome to the forums,
I'd be happy to help you out, but I would need a little more info on what you'd like to have happen.
does the player only create the particle (once) on each entering of a collider, or is it a repeating particle? and if so... does it repeat forever until player leaves trigger, or does particle happen for so many loops before it stops?
|
|
|
Post by vike1984 on Aug 8, 2016 1:24:23 GMT
hello ,yes I am looking for the particles to trigger only once on collider with the player.
|
|
|
Post by jgiroux on Aug 8, 2016 18:04:13 GMT
Damage Particle Trigger Effect requested by vike1984As promised Vike1984, here you go. 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.
|
|
|
Post by Invector on Aug 8, 2016 21:08:44 GMT
Can someone please give a cookie to this awesome user above?
|
|
|
Post by jgiroux on Aug 9, 2016 1:10:10 GMT
chocolate chip please ;)
|
|
|
Post by vike1984 on Aug 9, 2016 3:04:13 GMT
This code is actually very nice !!! easy to use and I can even add multiples particles on the same objects and place them thx jgiroux ! hehe
|
|
|
Post by jgiroux on Aug 9, 2016 11:12:39 GMT
glad to help Vike!
|
|
|
Post by orionuk on Sept 30, 2017 21:25:07 GMT
Blow cookies....had a CAKE on me :P
|
|