- Home /
Duplicate Question
Bullets will not fire forwards
I have a Javascript that is meant to make a gun fire but does not make the bullets fly forwards, they just drop. can anyone help?
Script:
pragma strict
var projectile : GameObject; var fireRate : float;
private var nextSpawnTime : float;
function Start () {
nextSpawnTime = Time.time + fireRate;
}
function Update () {
// left-click was pressed, launch a projectile
if ((Input.GetButtonDown("Fire1")) && (Time.time > nextSpawnTime)) {
// Instantiate the projectile at the position and rotation of this transform
Instantiate(projectile, transform.position, Quaternion.identity);
nextSpawnTime = Time.time + fireRate;
}
}
please SEARCH for 100s of basic beginner questions about projectiles, tutorials, unityGE$$anonymous$$S.com, etc etc etc
Answer by robertbu · May 01, 2013 at 05:35 AM
Assuming you are asking about a projectile that has a rigidbody, you have to add force to make your projectiles move. Plus you are not using the rotation of the transform. Try using these two lines instead of your Instantiate() line:
var go : GameObject = Instantiate(projectile, transform.position, transform.rotation);
go.rigidbody.AddForce(go.transform.forward * 2000);
This assumes that your projectile has a Rigidbody component.
Follow this Question
Related Questions
How do i make a fps where the gun always shoots realisticly 3 Answers
How to make a gun shoot? 2 Answers
How do I modify my script to give my gun recoil when I shoot? 1 Answer
How to add a delay to my script. 3 Answers
Bullets Based on Orientation 1 Answer