Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 frankyboy450 · Oct 27, 2013 at 01:44 AM · instantiatepositionrandomrotate

spawn muzzle flash with random rotation?

Hi. I have this code here GameObject flash = Instantiate(muzzleFlash, position, rotation) as GameObject;

and I'm trying to make it spawn the flash(MuzzleFlash) at the position and rotation of the bulletSpawn gameObject I have at the end of the barrel on my weapon. but unfortunately I can't figure out how to change the rotation to something random. Meaning that it rotates the X to something random.

I hope that made sense...

Comment
Add comment · Show 2
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 Owen-Reynolds · Oct 27, 2013 at 03:03 AM 0
Share

Just look up "unity random rotation y" (people seem to ask about Y more.) If you can't change the Y to an X (or you thought you wanted X, but it isn't doing what you thought,) ask something with those details.

avatar image Tomer-Barkan · Oct 27, 2013 at 04:33 PM 0
Share

You could use Quaternion.Euler(random, orig_y, orig_z) and pass it as the rotation to the instantiate:

 GameObject flash = Instantiate(muzzleFlash, position, Quaternion.Euler(Random.Range(0f, 360f), bulletSpawn.transform.eulerAngles.y, bulletSpawn.transform.eulerAgnels.z) as GameObject;

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Oct 27, 2013 at 02:21 PM

The old-and-good-but-no-more-available FPS Tutorial used an interesting approach: instead of instantiating the muzzle flash every shot (what allocates memory), the muzzle flash was an object childed to the gun and with its renderer disabled. When shooting, the mesh was rotated randomly about its local Z axis and the renderer was enabled for just one frame. You could use the same trick with this code, which should be part of the weapon script:

 public var muzzleFlash: Transform; // drag the muzzle flash object here
 
 private var muzzle = false; // muzzle flash enabled last frame?

 function Start(){
   // make sure the muzzle flash is initially disabled:
   muzzleFlash.renderer.enabled = false;
 }

 function TurnOnMuzzle(){
   // rotate the muzzle flash randomly about Z:
   muzzleFlash.Rotate(0, 0, Random.Range(0, 90));
   muzzleFlash.renderer.enabled = true; // turn on muzzle flash
   muzzle = true; // tell the code to turn it off next frame
 }      

 function TurnOffMuzzle(){
   if (muzzle){ // if muzzle flash enabled last frame...
     muzzleFlash.renderer.enabled = false; // turn it off
     muzzle = false;
   }
 }

TurnOnMuzzle must be called when you shoot - after checking ammo, shot interval etc. (flashing when you have no ammo is embarrasing...). Shooting or not, you must call TurnOffMuzzle every Update before doing anything in this script: this ensures that the muzzle flash lasts for just one frame:

 function Update(){
   TurnOffMuzzle(); // call this before anything in Update!
   // rest of Update
 }

   
Comment
Add comment · Show 2 · 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 frankyboy450 · Oct 28, 2013 at 12:31 AM 0
Share

Thanks for posting this. I couldn't find the old-and-good-but-no-more-available FPS Tutorial unfortunately. but knew that it had a good way to make the muzzle flash work. I think I'll probably change my code to enable/disable the renderer on the muzzle flash ins$$anonymous$$d of instantiating it as you said, it would take a lot of memory. I changed the values of Random.Range(0, 90) to (-30, 30) for some reason the muzzle flash would rotate to a 90 degree angle sideways.

I just copied this line: muzzleFlash.Rotate(0, 0, Random.Range(0, 90));

Into the script I have attached to the muzzle flash so that when it created is starts with a random rotation.

Thanks for the help ;)

avatar image Josh707 · Oct 28, 2013 at 01:25 AM 0
Share

Just a small tip to make it look nice without instantly disappearing, you can do something like this to fade the materials opacity to 0 and then turn off the renderer, and call a function to reset the opacity whenever the gun fires.

 function UpdateColor() {
     var renderers = transform.GetComponentsInChildren($$anonymous$$eshRenderer);
     var color = Vector4(opac,opac,opac,opac);
     for(var renderer : $$anonymous$$eshRenderer in renderers){
     renderer.material.SetColor("_TintColor", color);
     }
     
 }
 
 function Update() {
     if(firing == true){
     UpdateColor();
     opac -= Time.deltaTime * 50.0;
     if(opac <= 0.0){
         opac = 0.0;
         UpdateColor();
         firing = false;
     }
     }
 }


Then just have a function reset opac to 1.0 and firing to true.

avatar image
0

Answer by thenachotech1113 · Oct 27, 2013 at 04:02 AM

try something like this:

GameObject flash = Instantiate(muzzleFlash, position, rotation.x, Random.Range(1, 360), rotation.z) as GameObject;

i have not tried this code because i do not have unity installed in this pc, though im pretty sure something like that should do the trick. hope it helps

Comment
Add comment · Show 2 · 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 frankyboy450 · Oct 27, 2013 at 10:53 AM 0
Share

Sorry, there was an error: error CS1501: No overload for method Instantiate' takes 5' arguments I tried adding brackets "(")" around the rotations but that didn't work either. :/

avatar image Cherno · Oct 27, 2013 at 01:48 PM 0
Share

Just instantiate with Quaternion.identity, and then in the next line of code, put this:

         transform.Rotate(Random.Range(-5, 5), 0, 0, Space.Self);

This will rotate the instantiated object around the x axis, but not around the y and 7 axis. Just an example.

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

21 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

Related Questions

Camera rotation around player while following. 6 Answers

Instantiate a GameObject at the position of one of its child objects 1 Answer

Spawn GameObjects without overlap 1 Answer

Instatiating a prefab in a random position 0 Answers

Randomly position Instantiated GameObject's 1 Answer


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