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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by superp · Jan 24, 2014 at 08:22 AM · directionforce

shoot cannon in the direction barrel is facing

I am teaching myself unity 3d 4.3 so i can create 2D game. As a practice i am writing scorched earth. It is 2D and I rotate the gun barrel, around Z-axis. Lets say it is pointing to right, up 45 degree. Now I created a bullet as child of the barrel. I have code

 GameObject newBullet = (GameObject)Instantiate(originalBullet, originalBullet.transform.position, originalBullet.transform.rotation);
 
 newBullet.rigidbody2D.AddForce(new Vector2(1,1) * 250);

Original bullet is placed right in front of the barrel and i clone it. I am having problem with AddForce. Vector2(1,1) is hardcoded to 45 world-degree, but how do I get the direction of the barrel in relation to the world?

Also, is there a good example i can look at it for writing tank shooting game?

Thanks

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

1 Reply

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

Answer by NivF · Jan 24, 2014 at 10:53 AM

You should look at the barrel's transform to know it's direction. Depending on how you setup the barrel object it can be either transform.forward, transform.up or transform.right

If you need a Vector2 type then code should look something like:

Vector2 vBarrelDir = new Vector2(transform.up.x,transform.up.y);

newBullet.rigidbody2D.AddForce(vBarrelDir * 250f);

Comment
Add comment · Show 2 · 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 superp · Jan 25, 2014 at 05:02 AM 0
Share

thanks that did it. I still don't understand why it works. per doc, transform.up is green axis, which in my case Y-axis. why is getting .x and .y of y-axis give the direction i want? I believe the better question is, how do i tell which direction the barrel is facing? Is it usually z direction? in that case, shouldn't i have used transform.forward?

avatar image NivF · Jan 25, 2014 at 09:28 AM 0
Share

Yes, transform.forward would make more sense - but you have to realize unity can't really tell which direction is "forward" on any given mesh ... it's just a fancy name for the object's Z-Axis.

It really depends how your local coordinates are setup in the mesh. When you select your barrel object, you can see it's local coordinate system with the 3 colored arrows as the principal axes of that coordinate system.

If you are the one who made the model (in 3DS-$$anonymous$$ax/$$anonymous$$aya/Blender) you can easily rotate the object so it aligns with the Z-Axis. But it really is not required. There may be some unity scripts that can do that as well ...

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

19 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

Related Questions

How to change gravity's direction on a single gameobject? 0 Answers

how to apply a force in direction of object is facing? 2 Answers

Direction isn't changing after multiplying it *-1 0 Answers

Adding a force in the direction of an object. 2 Answers

Change direct force? 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