- Home /
How to change the speed of a projectile?
hi. im new to unity. i am creating a simple shooting game. I am using the unity code for a first person shooter (below), but want to know how i can change the speed of the bullet, and how to "turn off gravity".
var projectile : Rigidbody; var speed = 20;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
}
}
Thanks!!
Answer by Berenger · Feb 22, 2012 at 12:30 AM
About the speed of the bullet when it is shot, that's what the speed variable is for. While it's on air though, you'll need a reference on that bullet and do bullet.rigidbody.velocity *= something;
The gravity is in Physics.gravity.
Hmmm... I tried editing the speed variable, and it didnt work. Also, what do you mean by bullet.rigidbody.velocity *= something?
If you divide the velocity vector, the bullet will slow down. And as I think division is the tool of the devil, i prefere multiply by the inverse. I guess bullet.rigidbody.velocity *= 0.5 is more understandable.
Answer by aldonaletto · Feb 22, 2012 at 02:38 AM
You can set instantiatedProjectile.useGravity to false to disable gravity. About the projectile speed, it's defined by the variable speed, as @Berenger said. Once fired, the projectile keeps its velocity constant for the eternity (unless gravity is enabled or Drag isn't zero). var projectile : Rigidbody; var speed = 20; // this sets the speed in meters/second
function Update() { if( Input.GetButtonDown( "Fire1" ) ) { var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation ); instantiatedProjectil.useGravity = false; // disable gravity // set the bullet speed: instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider ); } }
Your answer

Follow this Question
Related Questions
Firerate problem 2 Answers
Grappling Hook 0 Answers
Trying to make an enemy fire at the player 1 Answer