Particle Manipulation scripts.

Ok so atm I have a script on a character. When you press jump it actives a little particle one shot system. Only problem is, it stays on. What I want it to do is only do one shot then turn off again. How would I do this?

/// This script moves the character controller forward 
/// and sideways based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
/// It is recommended that you make only one call to Move or SimpleMove per frame.    

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;

private var pe : ParticleEmitter;

function Start()
{
  pe = GetComponent(ParticleEmitter);
  pe.emit = !pe.emit;
}

function Update() {
    var controller : CharacterController = GetComponent(CharacterController);
    if (controller.isGrounded) {
        // We are grounded, so recalculate
        // move direction directly from axes
        moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                1);
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        
        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
			pe.emit = !pe.emit;
        }
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
    
    // Move the controller
    controller.Move(moveDirection * Time.deltaTime);
}

If its just a one shot particle system, I usually make it its own object as a prefab, make sure autodestruct is set on, and instantiate it at the point you want it to emit.

I was thinking of doing that, but I thought this would be better, I’ll probably do that in the end, I still want to know how to do this though (Want to expand by knowledge of scripting).

Well the problem with a one shot particle is it will repeatedly “loop” the one shot if you just set it to emit indefinitely, so the only other way besides using autodestruct would be to somehow determine the length of time it takes for your particle system to get from min energy to max energy to complete one loop, which is going to vary depending on your particle setup, and then you’d have to code a timer so it only emits for that duration.

I think you’d be better off using the prefab/autodestruct method :slight_smile: I believe thats pretty much what it was intended for anyway.