- Home /
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
}
}
Answer by Ejlersen · Dec 28, 2010 at 06:32 PM
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.
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
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;
}
}
- 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 :)
Okay, I give up. The code formatting on Unity Answers is like dancing with a elephant that insists on stepping on your feet.
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)
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092905im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with AIM to SHOOT script 1 Answer
enemy shoots with animation 1 Answer
how do i stop automatic firing? 3 Answers
How can i make an igniter that ignite objects? 1 Answer
unity script error 1 Answer