Guest User

Untitled

a guest
Sep 13th, 2015
25,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Press_Space_to_Fire : MonoBehaviour
  5. {
  6.     //Drag in the Bullet Emitter from the Component Inspector.
  7.     public GameObject Bullet_Emitter;
  8.  
  9.     //Drag in the Bullet Prefab from the Component Inspector.
  10.     public GameObject Bullet;
  11.  
  12.     //Enter the Speed of the Bullet from the Component Inspector.
  13.     public float Bullet_Forward_Force;
  14.  
  15.     // Use this for initialization
  16.     void Start ()
  17.     {
  18.    
  19.     }
  20.    
  21.     // Update is called once per frame
  22.     void Update ()
  23.     {
  24.         if (Input.GetKeyDown("space"))
  25.         {
  26.             //The Bullet instantiation happens here.
  27.             GameObject Temporary_Bullet_Handler;
  28.             Temporary_Bullet_Handler = Instantiate(Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation) as GameObject;
  29.  
  30.             //Sometimes bullets may appear rotated incorrectly due to the way its pivot was set from the original modeling package.
  31.             //This is EASILY corrected here, you might have to rotate it from a different axis and or angle based on your particular mesh.
  32.             Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
  33.  
  34.             //Retrieve the Rigidbody component from the instantiated Bullet and control it.
  35.             Rigidbody Temporary_RigidBody;
  36.             Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
  37.  
  38.             //Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
  39.             Temporary_RigidBody.AddForce(transform.forward * Bullet_Forward_Force);
  40.  
  41.             //Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being VERY generous here, normally 3 seconds is plenty.
  42.             Destroy(Temporary_Bullet_Handler, 10.0f);
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment