Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. public GameObject spawnPoint;           // Empty gameobject at the end of our gun so we know where to shoot the ray from
  2.  
  3.    
  4.  
  5.     public float lastFireTime;              // The last time we fired a shot so we can work out if were allowed to shoot           
  6.  
  7.    
  8.  
  9.     void Update () {
  10.  
  11.        
  12.  
  13.         // If we press our left mouse button
  14.  
  15.         if (Input.GetButton("Fire1")) {
  16.  
  17.            
  18.  
  19.             // If the last time we fired is more than 0.15 seconds ago
  20.  
  21.             if (Time.time - lastFireTime > 0.15) {
  22.  
  23.                
  24.  
  25.                 // Run the Fire function
  26.  
  27.                 Fire();
  28.  
  29.                
  30.  
  31.                 // Set our last fire time to now
  32.  
  33.                 lastFireTime = Time.time;
  34.  
  35.             }
  36.  
  37.            
  38.  
  39.         }
  40.  
  41.        
  42.  
  43.     }
  44.  
  45.    
  46.  
  47.     void Fire () {
  48.  
  49.         // If our raycast is set off all the data about what was hit will be stored in this variable
  50.  
  51.         RaycastHit hit;
  52.  
  53.        
  54.  
  55.         // This creates our raycast at spawnPoint.transform.position which is at the end of our gun
  56.  
  57.         // The 2nd one is the direction, for this it will be forward of our rays spawnpoint
  58.  
  59.         // The 3rd one is how long will the ray be, in this case its infinity
  60.  
  61.         // This wont effect the performance so dont worry
  62.  
  63.         if (Physics.Raycast(spawnPoint.transform.position, spawnPoint.transform.forward, Mathf.Infinity, out hit)) {
  64.  
  65.            
  66.  
  67.             // Get the tag from what we collided with and if its "Player"
  68.  
  69.             if (hit.collider.gameObject.tag == "Player") {
  70.  
  71.                
  72.  
  73.                 // How much damage to inflict
  74.  
  75.                 int damageInt = 10;
  76.  
  77.                
  78.  
  79.                 // This gets the gameObject of what we hit and sends a message to it
  80.  
  81.                 // Telling it to run a function called removeHealth() and send it a value of damageInt
  82.  
  83.                 // removeHealth a function of a script attached to your player
  84.  
  85.                 hit.collider.gameObject.SendMessage("removeHealth", damageInt);
  86.  
  87.             }
  88.  
  89.         }
  90.  
  91.     }
  92.  
  93.    
  94.  
  95.     // This will be an example of the function receiving the damage
  96.  
  97.     // This will be in a script on player
  98.  
  99.     // This will expect an int from what ever is calling it
  100.  
  101.     void removeHealth (int i) {
  102.  
  103.            
  104.  
  105.         // Remove the health from your player
  106.  
  107.         myPlayerHealthVar -= i;
  108.  
  109.            
  110.  
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement