- Home /
i need to mix this scripts, gun shoot
I want to mix scripts:
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 8;
var shootSound : AudioClip;
function Update()
{
if(Input.GetButtonDown("Fire1") && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forwar d * shootForce);
audio.PlayOneShot(shootSound);
shots++;
}
else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
{
shots = 0;
}
}
I like this because you have limit bullets , but with this script i cant delete the bullets(clone) but with this script i can:
var projectile : Rigidbody;
var speed = 20;
function Update () {
if ( Input.GetButton ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, 3);
}}
And i need that the bullet make a hole
Answer by aldonaletto · Sep 23, 2012 at 02:44 AM
This is easy: just call Destroy in the first script, right after creating the bullet:
...
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
Destroy(instanceBullet.gameObject, 3);
...
About the bullet hole: you must create it when the bullet collides with something. Add a script like this to the bullet prefab:
var holePrefab: GameObject; // drag the hole prefab here
function OnCollisionEnter(col: Collision){
Destroy(gameObject); // destroy the bullet
var contact: ContactPoint = col.contacts[0]; // get the first hit point
// calculate the hole position in front of the hit point:
var pos: Vector3 = contact.point + contact.normal * 0.005;
// find a rotation that aligns the plane with the surface:
var rot: Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
// instantiate the hole:
var hole: GameObject = Instantiate(holePrefab, pos, rot);
// "glue" the hole to the object hit by childing it:
hole.transform.parent = col.transform;
Destroy(hole, 30); // destroy the hole automatically after 30 seconds
}
The hole prefab itself is a small plane with an appropriate material. Use the Unity's Quad primitive to create a simple plane, set its size and assign to it a material with the Transparent/Cutout/Diffuse shader and the hole texture.
The first was very useful but the second script (Bullet hole) says: https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-ash3/564330_4661920196121_806735817_n.jpg
Your answer
Follow this Question
Related Questions
How do I make it so that my bullet projectile for my gun does damage? 1 Answer
shooting gun help 0 Answers
My reloading script not working after low bullets 2 Answers
Objects instantiate at prefab location instead of playtime location 1 Answer
Showing a dynamic amount of bullets in a Belt/Magazine/Cylinder 0 Answers