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 Brodie Winning · Oct 16, 2010 at 02:28 AM · prefabshootdiagonal

Shooting problem - bullet shoots diagonally when facing forward

I'm trying to create a simple shooting script and it's been succesfull, except when I'm facing forward he shoots diagonally to the right.It is a third person action game. Can someone help solve this? Below is my script.

var bulletPrefab:Transform; var shootForce = 1000;

function Update () { if(Input.GetButtonDown("Shoot")) { var instanceBullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); } }

Comment
Add comment · Show 3
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 · Oct 16, 2010 at 02:51 AM 0
Share

When you say, "when I'm facing forward" - do you mean the camera or the character? Is it a FPS?

avatar image FLASHDENMARK · Feb 05, 2011 at 09:39 PM 0
Share

I do believe that "Quaternion.identity" alligns after the the "World axes" I am not sure though, and "transform.rotation" alligns after the transforms x,y,z axes. Try replaceing "Quaternion.identity" with "transform.rotation" I am not a 100% sure though.

avatar image tool55 · Mar 03, 2011 at 08:41 PM 0
Share

Yes. OrangeLightning is right. You have to use transform.rotation if you want it to face the gameObjects foward axis rather than the world axis.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AVividLight · Oct 16, 2010 at 02:39 AM

Try attaching the script to the main camera... That might help...

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 Cyb3rManiak · May 11, 2011 at 10:25 AM

Vector3.forward will always return a Vector3 of (0,0,1). This is the forward direction relative to the world coordinate system. What you need is the relative forward of the bullet. Try this...

var instanceBullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
var v3RelativeForward: Vector3 = instanceBullet.transform.TransformDirection(Vector3.forward);
instanceBullet.rigidbody.AddForce(v3RelativeForward * shootForce)AddForce(v3RelativeForward * shootForce);

EDIT: When going over OrangeLightning's comment again, It just stuck me that you're also instantiating the bullet using Quaternion.identity, and just like he said, this means that the bullet's forward will also always be (0,0,1) all the time. I've edited your Instantiate() to use transform.rotation when instantiating the bullet. This will create the bullet aligned with whatever "transform" is pointing to. So if this is the character - the bullet will face to the character's forward direction as well, and the TransformDirection() will make sure that the force you add to it will be in that direction.

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

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

Enemy health. Need to take damage from two different bullet prefabs 1 Answer

is there a way to make a script prefab? 1 Answer

Tagging object while instating not working. 3 Answers

Disable & Re-enable Script 1 Answer

When flipping player, instantiated objects spawn on different spot. 2D shooting 0 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