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 · Feb 10, 2011 at 04:25 AM · shootfire

Spray Shot Script

I'm trying to make a bullet spray script but for some reason it only fires in a single direction, no matter how the character turns around. Any reason for this?

/// Add spread shot

    var rotation : Quaternion = Quaternion.identity;
    rotation.eulerAngles = Random.insideUnitSphere * 10;
    transform.rotation = rotation;
    var BulletShot = Instantiate(Bullet, transform.position, transform.rotation);
    BulletShot.rigidbody.AddForce(transform.forward * 1000);
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 GesterX · Feb 10, 2011 at 07:30 AM 0
Share

Does the shot always shoot in the same direction in the worldspace or is it always shooting "forward" dependant on the character rotation?

avatar image Persona · Feb 10, 2011 at 08:38 AM 0
Share

Same direction in world space

3 Replies

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

Answer by 3dDude · Feb 15, 2011 at 08:44 PM

Ok, if I get what your talking about... Then this should work:

var gunRecoil : float;

var BulletShot = Instantiate(Bullet, transform.position, transform.rotation);

var randomX = Random.Range(-gunRecoil,gunRecoil); var randomY = Random.Range(-gunRecoil,gunRecoil);

var dir = transform.TransformDirection(randomX,randomY,1000); BulletShot.rigidbody.AddForce(dir);

move the gunRecoil variable to the first line of the script.

Hope this helps!

Also, I would instead of setting force, set the velocity manually:

BulletShot.rigidbody.velocity = dir;

Also there might be some errors... I have not tried it in unity yet.

Hope this helps!

Comment
Add comment · Show 4 · 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 · Feb 18, 2011 at 04:08 PM 0
Share

Can you explain how this code works?

avatar image 3dDude · Feb 18, 2011 at 06:04 PM 0
Share

Ok, well ins$$anonymous$$d of trying to rotate the point where the bullets a created... Why not just move there velocity to a random x y? So what the code does is ins$$anonymous$$d of moving forward it creates a direction that sets the x and y component to be a random value some where between -gunRecoil and +gunRecoil. I have never used rigidbody force to move bullets so I don't know what it should be... But I am guessing something like 100. Please trying setting it to a wide range of numbers to test it.

Hope this helps!

avatar image Persona · Feb 19, 2011 at 03:01 PM 0
Share

Thanks for the help

avatar image 3dDude · Feb 19, 2011 at 05:50 PM 0
Share

Your welcome! Thanks for the points :D

avatar image
0

Answer by Bunny83 · Feb 16, 2011 at 04:12 AM

I guess this script is attached to your weapon?

By setting Transform.rotation the object will be rotated in world space. To rotate it relative to the parent use Transform.localRotation.
And second: you can assign eulerAngles but they don't wrap around automatically. You will get an error/warning when you try to set them to values outside [0..360]. Use the built in function Quaternion.Euler instead.

var rotation : Quaternion = Quaternion.Euler(Random.insideUnitSphere * 10);
transform.localRotation = rotation;
var BulletShot = Instantiate(Bullet, transform.position, transform.rotation);
BulletShot.rigidbody.AddForce(transform.forward * 1000);

ps. I like your way to calculate the spreading ;)
The z rotation is not really useful but a bullet don't have a top-side you have to keep up.
If this script is on your weapon, the weapon gets also rotated. if you don't want the z rotation, just set the z value to 0.

var rotation : Quaternion = Quaternion.Euler(Random.insideUnitSphere * 10);
rotation.eulerAngles.z = 0;
transform.localRotation = rotation;
[...]
Comment
Add comment · Show 4 · 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 · Feb 16, 2011 at 02:21 PM 0
Share

It fires off to the side.

avatar image 3dDude · Feb 16, 2011 at 02:56 PM 0
Share

Hey, did you try my script?

avatar image Bunny83 · Feb 16, 2011 at 03:30 PM 0
Share

$$anonymous$$ake sure that your instantiated objects don't spawn inside of another collider. Otherwise the objects will spread randomly. And to clear things up: forward have to be z-axis (blue), up is y-axis (green) and x-axis is right (red). When you test in the editor you can select the objects during the game. Switch to the scene view or tile the windows so you see both (game view and scene view).

avatar image Persona · Feb 16, 2011 at 08:24 PM 0
Share

I made sure. It looks like the firepoint is off by about 45 degrees

avatar image
0

Answer by Sebas · Feb 16, 2011 at 10:53 AM

A similar question has been answered before. Try this.

Comment
Add comment · Show 3 · 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 3dDude · Feb 16, 2011 at 02:57 PM 0
Share

The answer you gave on that question was similar to $$anonymous$$e.

avatar image Sebas · Feb 17, 2011 at 07:17 AM 0
Share

I'm aware of that. But for the sake of keeping things tidy, I prefer to use existing questions/answers as a reference. It should re$$anonymous$$d the person asking the question to do a thorough search first.

avatar image 3dDude · Feb 18, 2011 at 06:05 PM 0
Share

That indeed would be nice...

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

Firing flame balls with animation and particles 2 Answers

Audio when I shoot 3 Answers

Tap Where To Shoot 2D 1 Answer

Enemy Instantiating one bullet 3 Answers

Setting bullet instansiate direction? help? 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