im a noob at scripting in general but i want to play a particle effect at my feet once ive pressed right click i might be asking for more but as a noob i would like some kind of explanation to make it easy to understand the script
Simply add this script to your particle effect and space will toggle on and off.
using UnityEngine;
using System.Collections;
public class particleEffect : MonoBehaviour {
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Space))
gameObject.GetComponent<ParticleSystem>().enableEmission=!gameObject.GetComponent<ParticleSystem>().enableEmission;
}
}
It’s simple actually…
First make your particle system. You need to go to / GameObject => Create Other… => Particle System /
Then adjust particle settings in Inspector!
Then create a new script, I’m using JavaScript (or UnityScript) and Copy/Paste this command:
var particle : GameObject; // <- Here you drag your Particle System that is Attached to your Controller
function Start() // When the script loads, When Game Starts usualy
{
particle.particleSystem.enableEmission = false; // Particle OFF
}
function Update() // <- Update function (Every Frame/Always checking)
{
if(Input.GetMouseButtonDown(1)) // <- If Right(1) mouse button is DOWN and then...
{
particle.particleSystem.enableEmission = true; // <- Particle ON
}
}
So then add script to the player (Or empty game object in the scene that you will not delete) and then chose that Object / Player and in the in Inspector Drag’n’Drop your particle system that you created.
Then run the game. First your particle will not be there but when you press Right Mouse button Particle System will come up.
Hope this helps.
using UnityEngine;
using System.Collections;
public class ParticleController: MonoBehaviour {
public GameObject particle;
RaycastHit hit;
void Start ()
{
particle.particleSystem.enableEmission = false;
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetMouseButtonDown(0)){
if(Physics.Raycast(ray, out hit, 100f)) {
if (hit.collider.bounds.Contains(particle.transform.position)) {
particle.particleSystem.enableEmission = true;
}
}
}
}
}