Pressing a key starts a particle system

Hey,

I am using a character controller to move around and I also have a particle system. What I am trying to figure out is how can I make it so I can have my particle system start 10 feet in front of the character controller by pressing a key in the game mode. I dont have alot of experience with programming.

So far I have:

function Start()
{
	var emissionTime : float = 3.0;
	var emissionDelay : float = 3.0;
	var lastTime = 0.0;
}

function Update()
{
	if (Input.GetKeyDown (KeyCode.H))
	{
		GetComponent(Particlesystem).autodestruct = false;
		particlesystem.particle = true;
		lastTime = Time.time;
	}
	
	if (particlesystem.particle && (Time.time - lastTime) > emissionTime)
	{
		particleEmitter.particle = false;
	}
}

Im not sure if this is even close but would it be too much trouble if someone could write out the script or edit mine correctly?

Thanks

In future, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window before posting. When you don't mark your code as code, the text gets html escaped and it gets really hard to reformat it. I have formatted this one for you. Please check the code is the same as what you have.

–

Thanks for explaining how to format code. I will do that from now on.

–

1 Answer

1

Many suggestions to the different ways you could do this. The main one is just make the particle effect a child of your character, then everywhere the character goes, the particle effect goes. Then just toggle the emit value between true and false.

Start by just turning a particle effect on and off :

#pragma strict

var theEmitter : ParticleEmitter;
var isEmitting : boolean = true;

function Start() 
{
	// finds the particle emitter on this gameObject
	// change this to find your particle effect,
	// or simply comment out this line, and drop your particle emitter into the Inspector
	theEmitter = GetComponent( ParticleEmitter ); // finds the particle emitter on this gameObject
}

function Update() 
{
	if ( Input.GetKeyDown( KeyCode.H ) )
	{
		if ( isEmitting )
		{
			isEmitting = false;
			theEmitter.emit = false;
		}
		else
		{
			isEmitting = true;
			theEmitter.emit = true;
		}
	}
}

The other way for finding the position in front of the character is to multiply the player transform.forward by your distance :

var pointInFrontOfChar : Vector3 = playerChar.transform.forward * distance;

this is just pseudo-code. You need to store a reference to the GameObject that is the player character (playerChar), or just the transform.


I strongly recommend this tutorial series to all new Unity users (start at the bottom of the page then work up) : Unity 3D Student - 3d Modelling

I have added your script to the particle system. It works great! But I noticed that only the "ellipsoid particle emitter" only works and not the "particle system". Are those two seperate particle options? If so is there a way you could edit this for me to make the "particle system" work? This is all I would need you to do for me. Thanks for the tutorial link as well. I will check it out for sure.

–

Hey I am going to try this out and see if it works. Thank you for all of your help!

–