- Home /
Rocket slightly wrong trajectory
Hello Everyone,
I have a tank that can shoot rockets, except the rockets always faced one direction. So I added a script that makes the rockets' trajectory almost correct. Except the rockets always have a propensity to move along the Global Z-axis instead of the tank's z axis. It is not rare enough to be acceptable. How do I correct this issue?
Here's the script I attached to my rocket prefab:
function Update(){
var r = Quaternion.LookRotation(-rigidbody.velocity);
transform.rotation = r;
}
I have "-rigidbody.velocity" because otherwise the rockets are shot backwards...
My shooting code attached to the tank is:
var projectile=Instantiate(RPG, transform.FindChild("CannonPoint").transform.position, Quaternion.identity);
projectile.gameObject.tag="projectile";
projectile.rigidbody.AddRelativeForce(transform.forward*4000);
I appreciate your help. -Hyperion
Probably not the solution, but you should use rigidbody.AddForce() ins$$anonymous$$d of AddRelativeForce(). You want to launch the projectile along the same line as the tank's forward direction. It doesn't really matter what direction the projectile is oriented, and relative force accounts for the projectiles rotation. The only reason it isn't causing any problems is that you're instantiating it with Quaternion.Idenity'so relative directions are the same as absolute directions.
Answer by Hyperion · Aug 03, 2013 at 02:12 AM
OH SORRY!!! I found the problem. My "Cannon Point" had a collider and that caused all the trouble... Thanks for answering anyway.
Answer by robertbu · Aug 03, 2013 at 02:10 AM
You want AddForce() not AddRelativeForce().
projectile.rigidbody.AddForce(transform.forward*4000);
The vector 'transform.forward' is already a world coordinate, not a local coordinate, so you don't need to use 'relative'.
Thank you, I guess I didn't need to put in relative. It cleaned up my code a little.
Your answer
Follow this Question
Related Questions
rocketscience - how to find out what force is needed to hit a specific target 2 Answers
ballistic trajectory around a sphere 2 Answers
How to make thruster rotation 0 Answers
Missile Trajectory Change Course. 1 Answer
missile homing script help 1 Answer