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 tigshadorakie · Mar 06, 2014 at 04:44 PM · aiming

How to shoot a rocket in the direction object is facing

I just started playing around with 2D in Unity and I created a simple rocket that rotates in 360 degrees on the Z axis. I have made missle objects and have it setup so that that when they spawn they are pointing in the direction of the rocket. That part of things is all good. What I am not sure of is how to add force to the missle so that it shoots off in the direction it is facing. I have the AddForce code which takes a Vector2, but I really don't know what sort of equation to put in the function so that the force ratio changes depending on the rotation. I suspect that there is some sort of mathematical formula needed for this.

For example if the rocket is pointing straight then I would want only force applied along the x axis and absolutely nothing in the y. If I were pointing straight up I would want the opposite with only a y axis force added.

I am sure this has been done before as it is a pretty common game mechanic. I just wanted to use a lifeline and ask the audience before I go about trying to come up with a mathematical formula on my own.

Thanks again everyone for your help. You guys rock. Excellent support community which really helps us noobs.

Comment
Add comment · Show 1
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 tigshadorakie · Mar 06, 2014 at 08:12 PM 0
Share

Still need help with this if someone doesn't $$anonymous$$d. I believe Suribe didn't quite understand what I was asking.

3 Replies

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

Answer by Jiro-Ng · Mar 07, 2014 at 06:59 AM

There are many way to do this. The easiest way is this.

 missle.rigidbody.AddForce(rocket.transform.forward * _force);
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 Dblfstr · Mar 07, 2014 at 02:11 PM 0
Share

This is assu$$anonymous$$g all of his axis are setup correctly as I stated in my original answer. If the bullet prefab was build backwards, then it would shoot backwards. because its forward would be -x in relation to its world position.

avatar image tigshadorakie · Mar 07, 2014 at 05:18 PM 0
Share

Jiro, that works but I had to change it to rigidbody2D.AddForce(transform.right * force);

I am giving you credit for this answer. I am still unsure about things. I think I just need to get use to Vectors and how objects are oriented in the 2D or 3D space. $$anonymous$$uch thanks to you as this seemed like such a simple concept that I just could not figure out. I think I was trying to come up with the X and Y points of the Vector2 manually via a custom equation. I do have another question related to this now that it is working, but I will post it as a new question so that you or someone else has the chance to receive more credit.

avatar image
1

Answer by Dblfstr · Mar 06, 2014 at 10:04 PM

You should be able to use the built in Vector2 for your direction. Vector2.Up, etc. But those really depend on how you setup your prefab, and the object which Instantiates it. If you work in 2D, and your planes are all correct. +y is up, +x is right, etc. And your bullet prefab was created horizontal with the x axis, and faces the positive x direction. You should be able to use Vector2.right to shoot it 'forward'.

Try this:

     var BulletSpeed : float = 1000;
     var fwd : Vector3;
         var fwdxy : Vector2;

     function Start () 
     {
        fwd = transform.TransformDirection(Vector3.forward);
        fwdxy = (fwd.x,fwd.y);
        rigidbody2D.AddForce(fwdxy * BulletSpeed, ForceMode.Impulse); 
     }
Comment
Add comment · Show 7 · 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 Dblfstr · Mar 06, 2014 at 10:09 PM 0
Share

Also, see here: http://answers.unity3d.com/questions/362684/my-bullet-wont-move-forward.html It is javascript, but you should be able to get the general idea. This method used transformDirection, to deter$$anonymous$$e the direction. and then adds force in that direction

avatar image tigshadorakie · Mar 07, 2014 at 12:42 AM 0
Share

I have no problem shooting in the right direction. When I first spawn the rocket it is pointing in the right direction. It is the part where I am trying to apply force that is the problem. For example if I just set the AddForce to be purely horizontal the rockets will shoot straight and the rockets will be somewhat in the same starting angle the whole way. I am trying to find a way to change the x and y forces depending on the current angle of the rocket launcher/rocket so it gives an initial push in the same direction that it is already facing when spawned.

In other words I would need the exact amount of x force and y force applied to push at a certain angle. Am I making sense?

avatar image Dblfstr · Mar 07, 2014 at 02:26 PM 0
Share

Yes, and that is what transformDirection is for. It takes the local coordinates, and translates them to world space. (giving you the appropriate x and y coordinates.

  Transforms direction from local space to world space.
avatar image tigshadorakie · Mar 07, 2014 at 03:43 PM 0
Share

I get an error when using that code. Essentially it is saying that you can't use a Vector3 as the argument in the function and that it needs Vector2.

avatar image Dblfstr · Mar 07, 2014 at 03:54 PM 0
Share

Dangit, that's what I get for reading and not testing ;)

This is not real code* so

  decalre variable:
     var fwdxy : Vector2;
     
     in start:
     fwdxy = (fwd.x,fwd.y);
     
     rigibody2d.addforce(fwdxy*bulletspeed)

But I know I have placed a Vector3 in a function that needed a Vector2, the z coordinate was just omitted. Thinking I did it with raycasting or something.

Show more comments
avatar image
0

Answer by suribe · Mar 06, 2014 at 05:03 PM

You should get the direction your object is heading, and use that as the initial force vector.

Have you checked Vector2.Angle, or Quaternion.FromToRotation?

Anyway exactly how to do it will depend on your curent code. Post some showing how you handle your gameObject if you want a better answer.

Comment
Add comment · Show 1 · 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 tigshadorakie · Mar 06, 2014 at 05:11 PM 0
Share

Well, I really don't have much code and it is rather basic. Here is what I have. This is the start function of the instantiated missile.

 function Start () 
 
 {
 
 
 angle = transform.eulerAngles.z;
 Debug.Log(angle);
 
 rigidbody2D.AddForce(Vector2(0,0)); 
 
 
 }

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

Aiming down a gun 1 Answer

Cannot instantiate rotation on Prefab 1 Answer

AI rigidboy aiming 0 Answers

Help implementing aiming in sidescroller via mouse. 0 Answers

Problem getting "good-feeling" aiming with joysticks for a top-down shooter 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