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 Persona · Aug 01, 2010 at 02:17 AM · randombulletfire

Firing a curtain of bullets

I'm making a 2D shooter and wanted to know how to fire a curtain of bullets like in a bullet hell game from a single object. I'm trying to figure out how to alter a normal firing code to spread out in a fan shaped or random pattern if possible.

Edit: Code for firing //Fire rate of shots(Every # seconds, fire 1 shot) var fireRate: float =1.0; //Time to start shooting private var nextFire: float = 0.0;

function Awake() { // set our starting x position originalX = transform.position.x; } function Update(){ //Move forward //transform.position.x += 1 * speed * Time.deltaTime; // if the user just pressed the "Shoot" button down (edit->project settings->input) & if the time since the game started is greater than the next fire time //As long as the time the game starts and the fire rate = nextFire and is less the Time.time, it'll fire.

 if(Input.GetButton("Shoot")&&Time.time > nextFire)

{ audio.Play(); // create a shot, to the right of us (so it doesn't clip into our collision), with 90 degrees rotation nextFire = Time.time + fireRate; //Instantiate(Shot, transform.position + Vector3.right*2, Quaternion.Euler(0, 0, 90));

 Fire();
 }

var BulletPrefab: GameObject; var bulletSpeed = 100;

function Fire(){ var rotation : Quaternion = Quaternion.identity; rotation.eulerAngles = Random.insideUnitSphere; var bullet : GameObject; bullet = Instantiate(BulletPrefab,transform.position,rotation); bullet.rigidbody.velocity = bullet.transform.forward*bulletSpeed; }

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
2
Best Answer

Answer by spinaljack · Aug 01, 2010 at 10:06 AM

If you are using particles for bullets you can use the random velocity setting.

If you are using rigid bodies for bullets you can use Random.insideUnitSphere to get a random direction for it to travel in.

e.g.

var BulletPrefab: GameObject; var bulletSpeed = 100;

function Fire(){ var rotation : Quaternion = Quaternion.identity; rotation.eulerAngles = Random.insideUnitSphere; var bullet : GameObject; bullet = Instantiate(BulletPrefab,transform.position,rotation); bullet.rigidbody.velocity = bullet.transform.forward*bulletSpeed; }

If you wanted the bullets to move in a pattern then you can set the angle using some kind of math equation. Even very simple equations can result in interesting looking patterns. e.g. if you increment the y-rotation of the bullet fired by 10 degrees every frame they'll fan out like a spiral. If you then have a second set of bullets with their y-rotation incrementing in the opposite direction you'll get a double spiral that crosses over each other.

EDIT:

Changed rigid body to game object.

E.g. Maths:

yRotation : float = 0;

function IncrementAngle(){ yRotation += 5; if(yRotation>359){ yRotation = -359; } }

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 Persona · Aug 01, 2010 at 12:38 PM 0
Share

I'm getting an InvalidCastException: Cannot cast from source type to destination type. Can you give an example of the math formula as well? $$anonymous$$ath has never been my strong point.

avatar image spinaljack · Aug 01, 2010 at 03:14 PM 0
Share

Try that now...

avatar image Persona · Aug 02, 2010 at 01:15 AM 0
Share

this error came up: Actor::setLinearVelocity: Actor must be (non-kinematic) dynamic!

Also where do I put the math code? The update function moves it but can't have coroutines

avatar image spinaljack · Aug 02, 2010 at 01:26 AM 0
Share

That error means your bullet doesn't have a non-kinematic rigid body. You can put the increment code where ever you want.

avatar image Persona · Aug 02, 2010 at 02:31 AM 0
Share

Okay, I figured out that to make bigger changes in firing range I need to add to the random like this: rotation.eulerAngles = Random.insideUnitSphere * 25;

I mean how do I implement the math part, In the bullet script, in the player script?

Do I place Yrotation somewhere in the Instintate?

Show more comments

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

No one has followed this question yet.

Related Questions

shoot bullet at any direction(random) 0 Answers

Random enemy fire rate 4 Answers

How can I shooting script c#? 0 Answers

Destroy Bullet on Random Collision 0 Answers

Spreading Fire (Bullets) 3 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