FPS Script Issues

Ok, so i am making a FPS, and im having trouble with firing the actual projectile. the script i currently have is

var projectile : Rigidbody; var speed = 20;

if (Input.GetButtonDown (“Fire1”)) {

var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

}

velocity = transform.TransformDirection(Vector3 (0,0,speed)); Physics.IgnoreCollision(collider, transform.root.collider);

When i click the fire button, a projectile come out but it simply drops to the ground instead of shooting forwards. Help?

You should use the physics engine instead of the transform engine when firing a bullet:

First of all make sure you have a rigidbody attached to the bullet prefab. Then make sure that "useGravity" is checked off in the inspector. This will keep the bullet from droping down.

var projectile : Rigidbody; 
var speed : float = 20;

if (Input.GetButtonDown ("Fire1")) {
var instantiatedProjectile:Rigidbody = Instantiate(projectile,transform.position,transform.rotation);
instantiatedProjectile.rigidbody.AddRelativeForce(transform.forward * speed);
}

Physics.IgnoreCollision(collider, transform.root.collider);

Also, I don't know what your using ignore collision for but you should only use it if you want the availability of the collision between the two objects to switch on and off.

If not you should assign each object to a different layer, go into Edit/Project Settings/Physics, and check the collisions between the two layers off.

And Why is your question in BOLD? :) Hahahaha

There are two things you should for this to work.

  1. this line:

    var instantiatedProjectile : Rigidbody = Instantiate (STUFF);

the function Instantiate returns a GameObject, NOT a Rigidbody. Because of this, you cause the program to do a large number of checks and unsafe operations that you don’t need. you should change it to

var instantiatedProjectile : GameObject = Instantiate (STUFF);
  1. You have now created your projectile, but it has no velocity! You need to do something such as

    instantiatedProjectile.rigidbody.velocity = this.transform.forward * speed;

or this

instantiatedProjectile.rigidbody.AddForce(this.transform.forward*speed, ForceMode.Impulse);

This makes your projectile have momentum and move forward after it is created.

If your projectile is being affected by gravity and you don’t want that, you can either go to your prefab and change the UseGravity flag, or say

instantiatedProjectile.rigidbody.useGravity = false;

make sure you do all of these operations INSIDE of the if(){brackets}

also, in the future use the “101010” button to format your text as code.