x


Flamethrower in 2D?

I have top-down shooter (Something like dead frontier) and i want to make a flamethrower but i dont know how so please if you know how then please help me. I know i have to have a particle emitter but i dont know how to make the scripts. I have a basic gun that shoots and i used this script:

using UnityEngine;
using System.Collections;

public class BulletScript : MonoBehaviour {
    private float moveSpeed = 30f; 
    private float timeSpentAlive; 
    private GameObject objPlayer;
    private VariableScript ptrScriptVariable;

    // Use this for initialization
    void Start () {
        objPlayer = (GameObject) GameObject.FindWithTag ("Player");
        ptrScriptVariable = (VariableScript) objPlayer.GetComponent( typeof(VariableScript) );
    }

    // Update is called once per frame
    void Update () {
        timeSpentAlive += Time.deltaTime;
        if (timeSpentAlive > 1) 
        {
            removeMe();
        }
        // move the bullet
            transform.Translate(0, 0, moveSpeed * Time.deltaTime);
            transform.position = new Vector3(transform.position.x,0,transform.position.z); // because the bullet has a rigid body we don't want it moving off it's Y axis
    }
    void removeMe ()
    {
        Instantiate(ptrScriptVariable.parBulletHit, transform.position, Quaternion.identity );
        Destroy(gameObject);
    }
    void OnCollisionEnter(Collision Other)
    {
        if ( Other.gameObject.GetComponent( typeof(AIscript) ) != null && Other.gameObject != objPlayer ) // if we have hit a character and it is not the player
        {
            AIscript ptrScriptAI = (AIscript) Other.gameObject.GetComponent( typeof(AIscript) );
            ptrScriptAI.health -= 10;
            Instantiate(ptrScriptVariable.parAlienHit, transform.position, Quaternion.identity );
            removeMe();
        }
        removeMe(); // remove the bullet if it has hit something else apart from an enemy character
    }
}
more ▼

asked Dec 28 '10 at 03:48 PM

mido555 gravatar image

mido555
74 8 8 14

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You are absolutely correct that you need a particle emitter for this :) Lets take a look at it together, but you need to finish it yourself.

  1. Add the "flame thrower" from GameObject -> Create Other -> Particle System.
  2. Lets set some test values:

    • Ellipsoid Particle Emitter:
    • Emit = false
    • Min Size = 0.5
    • Max Size = 0.7
    • Min Energy = 0.8
    • Max Energy = 1.0
    • Min Emission = 10
    • Max Emission = 13
    • Local Velocity = (0, 0, 2)
    • Ellipsoid = (0, 0, 0)
    • Particle Animator
    • Size grow = 3
    • Colors = Give it some cool flame colors or try the standard assets for flames
    • Particle Renderer
    • Cast/Receive shadows = false
  3. We should have some sort of primitive flame thrower now, but we still need to turn it on/off, so how about some scripting:


    using UnityEngine;
    using System.Collections;
public class FlameThrower : MonoBehaviour
{
    private ParticleEmitter particleEmitter = null;
    private ParticleAnimator particleAnimator = null;  

    void Awake()
    {
        // Get the particle emitter or add it
        particleEmitter = GetComponent<ParticleEmitter>();

        if (!particleEmitter)
            particleEmitter = gameObject.AddComponent<ParticleEmitter>();

        // Get the particle animator or add it
        particleAnimator = GetComponent<ParticleAnimator>();

        if (!particleAnimator)
            particleAnimator = gameObject.AddComponent<ParticleAnimator>();
    }

    void Update()
    {
        if (Input.GetButton("Fire1"))
            particleEmitter.emit = true;
        else if (Input.GetButtonUp("Fire1"))
            particleEmitter.emit = false;
    }
}

  1. Hm, how to detect damage. Well one way of doing it is to use some sort of collider/trigger for it. Turn on/off a box collider, which is the same size of the flames should do it. To detect damage we then use OnCollisionEnter(...) or OnCollisionStay(...).

Hope this helps you on your way :)

more ▼

answered Dec 28 '10 at 06:32 PM

Ejlersen gravatar image

Ejlersen
1.3k 1 6 11

Okay, I give up. The code formatting on Unity Answers is like dancing with a elephant that insists on stepping on your feet.

Dec 28 '10 at 06:42 PM Ejlersen

Thanks alot man this helped out alot :DDD

Dec 28 '10 at 09:44 PM mido555

Glad that I could help :)

Dec 28 '10 at 09:45 PM Ejlersen

Hey man i got to the scripting part and i got this error:

Assets/FlameThrower.cs(13,23): error CS0411: The type arguments for method `UnityEngine.Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly

sorry if this is annoying but i am a complete newbie and i dont know very much about anything in unity. (I do know the basics i had unity for about 2 weeks now)

Dec 28 '10 at 10:09 PM mido555

No problem. You can use a box collider for doing damage, which you can turn on/off. Try setting as trigger and use the OnTriggerStay(). If an enemy is within the trigger, then do some damage.

Dec 30 '10 at 01:45 PM Ejlersen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3416
x330
x153
x6

asked: Dec 28 '10 at 03:48 PM

Seen: 1482 times

Last Updated: Dec 28 '10 at 04:33 PM