# Projectile trajectory velocity and angle instead of force with a rigidbody

 0 I'm currently using myRigidBody.AddForce(x,y,z) to move an object. I'm guessing that the three values in AddForce are in Newtons(kg m/s^2) I'm not sure. I've been asked to change the program to launch the projectile with and angle and initial velocity. I have not yet found anything in the scripting documentation that specifically mentions using angle and velocity and I don't know a way to work backwards from initial velocity and angle to get three vectors of force. more ▼ asked May 03 '10 at 12:46 PM rd42 296 ● 30 ● 37 ● 46 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 The X,Y,Z values represent a Vector, whose direction is the angle, and whose length is the velocity. However the AddForce function has another version which accepts a vector explicitly rather than having a parameter for each of the x,y & z values. In addition, if you're applying a force which is the equivalent of a single push at an instant in time (rather than a push over a duration of time), you should use a special force mode called "Impulse". You can apply a force in the direction of a particular gameobject by using that gameobject's transform.forward property. For example, to launch a projectile in the direction of the gameobject on which the script is placed, you could use something like this: ``````using UnityEngine; public class FireTest : MonoBehaviour { public Transform bulletPrefab; public float shotPower = 80; void Start() { // wait 10 secs, then fire every 5 seconds InvokeRepeating("Fire", 10, 5); } void Fire () { // the starting conditions (the position and angle of the 'gun' object) Vector3 startPos = transform.position; Quaternion shotAngle = transform.rotation; // create the projectile object Transform bullet = (Transform)Instantiate(bulletPrefab, startPos, shotAngle); // apply the firing force bullet.rigidbody.AddForce(transform.forward * shotPower, ForceMode.Impulse); } } `````` more ▼ answered May 03 '10 at 01:59 PM duck ♦♦ 41k ● 92 ● 148 ● 415 For some reason it's not compiling, it says ';' expected. It's on this line -> Vector3 startPos = transform.position; and it has a semicolon?! I don't understand. May 05 '10 at 01:19 PM rd42 Are you using Javascript or C#? (the example is c#). Also, it could be an error on one of the lines before the one that it's saying - did you miss a semicolon on the line immediately before that line? May 05 '10 at 07:51 PM duck ♦♦ I tried attaching your exact code to a sphere gameobject with a rigidbody component. No extra script and still the ';' expected error. I also tried putting it inside function Update() . I'm not sure what I'm still doing wrong. thanks for your help. May 07 '10 at 03:38 PM rd42 ok, you didn't answer whether you're using C# or JS (that's important). I updated the example to make it more complete - it's now a complete usable C# class. May 07 '10 at 03:48 PM duck ♦♦ I've been using JS, maybe that's why? I think the file name was .js. I'll try it again as C# May 10 '10 at 02:23 PM rd42 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 well if you have the velocity that have its magnitud, and you have an angle you can transform that in a vector this means for trigonometry that velocity*sin(angle) will give you the velocity vector in the y axis and velocity*cos(angle) will give you the velocity vector in the x axis. i'm also using the force in unity to move the objects, but now i see that the velocity would be easier for the future use, so im changin to that =P good luck more ▼ answered Dec 17 '10 at 11:32 PM poncho 932 ● 2 ● 3 ● 14 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x3733
x1869
x318
x286
x253

asked: May 03 '10 at 12:46 PM

Seen: 6893 times

Last Updated: May 03 '10 at 12:46 PM