Shoot Script Help

Recently I have been using a shoot script, but it has not been working out. Can someone tell me how to make a script where you shoot a prefab, but i want it so it shoots forward and never falls low or falls to the side. It just goes straight forward and only stops when it hits a collider of any kind.

Hi, you can use the ShootProyectile.js script attached to a non rendered gameobject placed has a child of your weapon.

Recomended:

To avoid the missing collisions of the bullet at fast speeds, use this script attached to the bullet prefab.


For the shoot, create a new Javascript object, clear its default content and copy the code above, then name it "ShootProyectile" or wathever you want, your bullet prefab must have Rigidbody, then attach the code to your not rendered gameobject (this object will be the point of shoot), placed as a child of your weapon.

If you want the bullet to go straight, just disable "Gravity" on the Rigidboy of the bullet, at the inspector:

NOTE: I think is better to use Raycast for general shooting, and just for granades and/or misils you can use the ShootProyectile script.

Also if you will use the DontGoThroughThings.js (recomended to hit colliders at fast speeds with a fisic bullet) script on your bullets, go to Edit, Physics, and disable "Raycasts Hit Triggers", to avoid the bullets collide on triggers, or if you want to keep this option able, use a specific layer only for triggers, then unset this layer on the layermask variable of the DontGoThroughThings.js script.

ShootProyectile.js Script above:

var shotsound:AudioClip;
var shellPrefab : GameObject;
var proyectilevelocity:int;
var ammo:int;
var fireRate = 1.0;
private var nextfire = 0.0;
function FixedUpdate () {
    if(Input.GetButton("Fire1") && Time.time>nextfire && ammo>0)
        ShotProyectile();
}
function ShotProyectile(){
        if(Time.time > nextfire)
        {
    var Grenade:GameObject = Instantiate(shellPrefab,transform.position,transform.rotation);
    Grenade.GetComponentInChildren(Rigidbody).velocity =transform.TransformDirection(Vector3(0,0,proyectilevelocity));
    //Physics.IgnoreCollision(shellPrefab.GetComponentInChildren(BoxCollider).collider,transform.parent.collider);
    ammo--;
    if(shotsound)
        audio.PlayOneShot(shotsound);
    Destroy(Grenade,10);
    nextfire = Time.time+fireRate;}
}

Good Luck!