Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by isavbherman · Aug 30, 2018 at 02:03 AM · particlesparticlesystem

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
         }
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image isavbherman · Aug 30, 2018 at 01:28 PM 0
Share

@ifurkend hey this isnt working. i copy this on my script but nothing happens

avatar image ifurkend isavbherman · Aug 31, 2018 at 01:57 AM 0
Share

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.

avatar image ifurkend · Aug 31, 2018 at 06:34 AM 0
Share

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.

avatar image isavbherman ifurkend · Sep 01, 2018 at 01:44 PM 0
Share

When i pressed play just spawned a orb but when I click not happens. what im doing wrong??

alt text

tut.jpg (134.7 kB)
avatar image ifurkend isavbherman · Sep 01, 2018 at 02:05 PM 0
Share

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.

Show more comments
avatar image ifurkend · Sep 01, 2018 at 10:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges