- Home /
Projectile trajectory velocity and angle instead of force with a rigidbody
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.
Answer by duck · May 03, 2010 at 01:59 PM
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);
}
}
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.
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?
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.
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.
I've been using JS, maybe that's why? I think the file name was .js. I'll try it again as C#
Answer by poncho · Dec 17, 2010 at 11:32 PM
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