- Home /
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.
Still need help with this if someone doesn't $$anonymous$$d. I believe Suribe didn't quite understand what I was asking.
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);
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.
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.
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);
}
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
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?
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.
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.
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613135534im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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