• 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 DMCH · Dec 28, 2012 at 05:04 PM · 2dangle

2D Top-down Shooter: Shotgun Bullets Spread

Hello,

Having some trouble with getting a consistent spread on shotgun bullets, regardless of the direction the character is facing. What I have is fine if the character is facing vertically, but the bullets clump together when facing horizontally. Here's what I've got so far.

EDIT: The distance the mouse is from the character changes the input rotation var, and consequently affects the bullet's spread.

I'm fairly new to unity, and only understand the basics of quaternions, so a simple solution would be great. Thanks for your time!

 if(currentWeaponType == "Shotgun")
 {
             Debug.Log("PlayerScript.HandleBullets(): Shotgun Fired. Input rotation is:" + inputRotation);
             
             Vector3 bullet2Angle = inputRotation;
             bullet2Angle.x -= 15;
             bullet2Angle.z -= 15;
             
             Vector3 bullet2Vector = tempVector;
             bullet2Vector.x -= 0.3f;
             bullet2Vector.z -= 0.3f;
             
             Vector3 bullet3Angle = inputRotation;
             bullet3Angle.x += 15;
             bullet3Angle.z -= 15;
             
             Vector3 bullet3Vector = tempVector;
             bullet3Vector.x += 0.3f;
             bullet3Vector.z += 0.3f;
             
             
             GameObject bullet1 = (GameObject) Instantiate(bullet, tempVector, Quaternion.LookRotation(inputRotation));
             GameObject bullet2 = (GameObject) Instantiate(bullet, bullet2Vector, Quaternion.LookRotation(bullet2Angle));
             GameObject bullet3 = (GameObject) Instantiate(bullet, bullet3Vector, Quaternion.LookRotation(bullet3Angle));
             
             
 }
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
0
Best Answer

Answer by jeango · Dec 28, 2012 at 06:16 PM

I would tackle the problem another way. I take it you have a shotgun prefab of some sort. You could attach 3 gameObjects to that prefab, each one facing at the desired angle of each bullet. Then you can attach to each of those 3 gameObjects a script that would spawn bullets.

Or, if you still want to play with rotations, since you're making a 2D game, I believe you could use EulerAngles instead of Quaternions since one of your axis will never move (the main reason for Quaternions being to avoid Gimbal lock, which doesn't occur in a 2D world).

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Euler.html

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 DMCH · Dec 28, 2012 at 06:40 PM 0
Share

Thanks. I'll try creating gameobjects as spawnpoints for the bullets. Not using prefabs for weapons - have a non-mono class with a constructor to build the weapons, and a Weapon object array in the character class. I'll accept the answer as soon as I can get your solution to work. Thanks again for the help!

avatar image DMCH · Dec 29, 2012 at 03:07 AM 0
Share

Thanks, adding child game objects to the character as spawn-points for bullets worked, and is a simple solution.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Finding the angle between 2 clicked points 2 Answers

How do i get a attack offset of the player at a fixed distance in 2D? 1 Answer

Drag and jump mechanics? 2 Answers

How do I fire a 2D projectile based on it's current angle/location. 1 Answer

follow an object's x-axis and have it translate smoothly into another object's z-axis rotation 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