• 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
Question by Kazaskater · Dec 03, 2010 at 04:21 AM · rotationraycastinstantiatequaterniongun

Instantiate not shooting in right direction

I am trying to make a gun shoot bullets, and I have this code:

var bulletcreate = Instantiate(bulletprefab, GameObject.Find("Cylinder").transform.position, Quaternion.identity);
bulletcreate.rigidbody.AddForce(transform.forward * bulletspeedm4);

I have the bullet shooting out of the right spot, but when I shoot, it shoots out to the left. I want it to shoot out straight, right in the center of the screen. also, the cylinder in the code is the point where the bullet spawns from, then launches forward.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Rennat · Dec 03, 2010 at 04:53 AM

When you use Quaternion.identity you are making the object align with the world, to make it align with the GameObject that this script is on use transform.rotation instead.

Things that could mess you up are

  • your character/camera is not facing forward (positive Z axis)
    If your character is built facing in the negative X direction then
  • your script is on a game object that is not rotating with the camera (transform is the Transform of the GameObject that the script is on)
    to use the transform of the camera add a var mainCamera : Transform; property to your script and link it to the camera in the inspector.

Comment
Henrik Poulsen

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Setting rotation of instantiated object to that of a raycast? 1 Answer

Rotating a certain axis offsets the other ones? 1 Answer

Setting rotation parallel to ground 1 Answer

Why are my guns shooting lower when my character moves forward? 1 Answer

Gun not rotating with the camera? 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