Guest User

Untitled

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