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 smashingy0u · Mar 06, 2014 at 11:49 PM · c#camerarigidbodyvector3bullet

vector3.forward doesn't work when used in Rigidbody?

Hello I'm trying to get a ball shooting in the direction that the camera is looking towards. I now got the sphere to spawn facing the direction the camera is facing but when i use bullet.rigidbody.AddForce(Vector3.forward * 1000); it will simply shoot the sphere in the world's Z axis direction.

complete code

 public class Player : MonoBehaviour
 {
 
     GameObject bullet;
     public float power;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.J))
         {
             Fire_Bullet();
         }
     }
 
     void Fire_Bullet()
     {
         Debug.Log("test");
         bullet = (GameObject)Instantiate(Resources.Load("Bullet"), transform.position, transform.rotation);
         bullet.rigidbody.AddForce(Vector3.forward * 1000);
     }
 
 }
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

2 Replies

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

Answer by LukaKotar · Mar 06, 2014 at 11:50 PM

Try bullet.transform.forward instead.

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 smashingy0u · Mar 07, 2014 at 12:08 AM 0
Share

Thanks This works perfectly! is this working because it has something to do with local/global axes?

avatar image YourGamesBeOver · Mar 07, 2014 at 12:34 AM 0
Share

that is precisely why. Unity globally defines "forward" as the positive z direction.

avatar image LukaKotar · Mar 07, 2014 at 06:30 PM 0
Share

Vector3.forward is the global (world space) Z axis (0,0,1), and transform.forward is the local Z axis (based on the objects rotation). This is why - as mentioned in the question - it flew along the Z axis in world space. Apologies for late reply.

avatar image
0

Answer by Cognitive Dissonance · Mar 07, 2014 at 12:31 AM

I think forward is the (0,0,1) vector which is the Z direction. Im not sure i understand the direction you want to shoot at. If its the x axis i think you use the right vector and if its the y axis the up vector.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# - Offset In-Air Camera from Current Object Position 1 Answer

Change player movement 0 Answers

CS1502 and CS1503 Error 0 Answers

Launching a Character controller using vectors 0 Answers

How to make a ball stay in the air longer when force is added? 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