Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FlammingFart · Feb 21, 2013 at 02:47 PM · bulletgunraycasthit

bullet prefab always come out in wrong direction

i created a cube with rigidbody as a bullet prefab but then the bullet prefab always come out in wrong direction what could be wrong? please help

heres my code:

 var prefabBullet:Transform;
 
 var shootForce:float;
 
 var shots : int = 0;
 
 var maxShots : int = 8;
 
 var shootSound : AudioClip;
 
 function Update()
 
 {
 
 if(Input.GetButtonDown("Fire1") && shots < maxShots)
 
 {
 
 var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
 
 instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
 
 Destroy(instanceBullet.gameObject, 5);
 
 audio.PlayOneShot(shootSound);
 
 animation.Play("gunidle");
 
 shots++;
 
 }
 
 else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
 
 {
 
 animation.Play("gunshots");
 
 shots = 0;
 
 }
 
 }
Comment
Add comment · Show 8
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 CeejayZSmith · Feb 21, 2013 at 03:31 PM 0
Share

Which direction does it go in? Is it always to a certain point in the scence of always going to the left/right ect

avatar image robertbu · Feb 21, 2013 at 03:55 PM 0
Share

What is the "wrong" direction. It looks like you are always firing it towards the positive Z axis.

avatar image FlammingFart · Feb 21, 2013 at 04:27 PM 0
Share

CeejayZSmith - forward/backward

robertbu - because in the rigidbody of the bullet i freeze all the rotation and in position i freeze only the x and y

avatar image robertbu · Feb 21, 2013 at 04:50 PM 0
Share

What we need to understand is what you want to happen. Your code shoots the bullet down the positive Z axis. Did you want it to shoot towards the negative Z axis or somewhere else?

avatar image FlammingFart · Feb 21, 2013 at 05:08 PM 0
Share

i want it to be like a real gun... at any direction i point the gun the bullet will come out

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by FlammingFart · Feb 21, 2013 at 07:20 PM

in the cube which is the bullet prefab... i put a rigidbody and i freeze its position x and y not the "z" as well as the rotation x, y and z

but then the bullet prefab came out either forward and backward only

Comment
Add comment · 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
0

Answer by robertbu · Feb 21, 2013 at 07:31 PM

If you are just trying to make a bullet come out of gun, I suggest.

  1. Don't freeze the x and y movements in your prefab.

  2. Put an empty game object just in front of the barrel of the gun. This will be the spawn point for the bullets and (I'll call it goSpawn).

  3. Rotate the empty object so that it's rotation matches the barrel of the gun. Note the positive Z direction is forward.

  4. Make the empty game object a child of the gun by dragging it on top of the gun in the Hierarchy view.

  5. When calling Instantiate, use the position and rotation of goSpawn.

    var instanceBullet = Instantiate(prefabBullet, goSpawn.transform.position, goSpawn.transform.rotation); instanceBullet.rigidbody.AddForce(transform.forward * shootForce);

Comment
Add comment · Show 5 · 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 FlammingFart · Feb 21, 2013 at 08:24 PM 0
Share

what about the goSpawn? theres an error about the unknown identifier goSpawn

avatar image robertbu · Feb 22, 2013 at 01:42 AM 0
Share

goSpawn is the spawn point...the empty game object. If you attach the bullet spawning script to the spawn point, you can get its values from "transform.position" and "transform.rotation" rather than goSpawn. If you are spawning from another script, you will need to declare the variable and initialize it somehow...GameObject.Fing() or by dragging a game object in the inspector.

avatar image Kzapas · Jan 28, 2020 at 11:25 PM 0
Share

Thank you!!!!

avatar image Dustin111 · Apr 29 at 07:33 AM 0
Share

doesn't work

avatar image Captain_Pineapple Dustin111 · Apr 29 at 07:53 AM 0
Share

This is really not helpful @Dustin111 In general the process described in the answer is a good way to approach this issue.

If you think something in there is not correct then please rather add a suggestion of what someone could do better.


If you face a problem with your setup in your own project then please open a new question with a detailed description of your current setup.

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

14 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

Related Questions

Bullet Collision Simulation? 2 Answers

Bullets Based on Orientation 1 Answer

How to make a gun shoot? 2 Answers

Shooting. Bullet floats and sprays 1 Answer

Troubles With A Shoot Script 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