- Home /
How to shoot particle like projectile?
hi everyone I made a tank that shoot sphere balls on mouse click.
my C# script:
GameObject prefab;
// Use this for initialization
void Start () {
prefab = Resources.Load("projectile") as GameObject;
}
// Update is called once per frame
void Update() {
if (Input.GetMouseButtonDown(0))
{
GameObject Orb = Instantiate(prefab) as GameObject;
Orb.transform.position = transform.position + Camera.main.transform.forward * 2;
Rigidbody rb = Orb.GetComponent<Rigidbody>();
rb.velocity = Camera.main.transform.forward * 40;
}
}
but i want to shoot a particle that i created. how can I do that? I trying change my scripts but this didnt work.
Answer by ifurkend · Aug 30, 2018 at 03:51 AM
Once you instantiate your effect prefab to a clone game object in your game, you need to get the particle system component. In many cases, instantiate any prefab in Update() is a bad idea (destroying game object repeatedly can potentially cause memory leak), instead just do it either on Awake, Start or Enable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FireParticlesOnMouseDown : MonoBehaviour {
public Transform effectPrefab;
public Camera mainCamera;
private Transform _clone;
private ParticleSystem _clone_ps;
private ParticleSystem.EmissionModule _clone_ps_em;
void OnEnable () {
//"Camera.main" is pricey, use it only when you run out of options.
if (!mainCamera) {
mainCamera = Camera.main;
}
_clone = Instantiate(effectPrefab);
_clone.parent = transform;
_clone.localPosition = Vector3.forward * 2f;
_clone.localRotation = Quaternion.identity;
_clone_ps = _clone.GetComponent<ParticleSystem>();
_clone_ps.Play();
_clone_ps_em = _clone_ps.emission;
_clone_ps_em.enabled = false;
}
void Update () {
if (Input.GetMouseButton(0)) {
_clone_ps_em.enabled = true;
//Calling ps.Play during Update will cause the effect to reset itself repeatedly.
} else {
_clone_ps_em.enabled = false;
}
}
}
@ifurkend hey this isnt working. i copy this on my script but nothing happens
I tested it myself. Just make sure your particle system is:
Looping
Constant emission rate over time ins$$anonymous$$d of burst.
Particles are emitting in the positive z-axis direction.
Also remember this is a self-containing code, just save this as a new script and name it “FireParticlesOnNouseDown.cs” or the file name should be the same as the class name.
When i pressed play just spawned a orb but when I click not happens. what im doing wrong??
When I looked at your original code, I thought you were planning to do a first person shooter so it is parented to the main camera transform (as my script was meant to be added to the camera game object). But if you want the orb to be fired from the turret, just change the "public Camera mainCamera" to "public Transform turret", remove the whole assigning Camera.main if statement and parent the effect clone to your turret object (change "_clone.parent = transform;" to "_clone.parent = turret;). If the orb still doesn't act properly, make sure the orb particle system has some positive start speed.
I am kinda confused why you orb seems to be spawned at an unexpected position, supposedly it is overridden by the Vector3.forward*2f. I suppose you have already assigned the cannon to the turret variable (laser-head_low). Can you show the full settings of your orb prefab particle system?
Your answer
![](https://koobas.hobune.stream/wayback/20220612173740im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I create a particle 'vortex' or implosion using Shuriken? 2 Answers
Can I slow down a Particle System? - e.g. Slow Motion Effect 1 Answer
How to Slowly Fade Particle System via Script 2 Answers
Particle sorting/layering question 0 Answers
Spawned Particle System in game looks different from prefab 2 Answers