Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public GameObject spawnPoint; // Empty gameobject at the end of our gun so we know where to shoot the ray from
- public float lastFireTime; // The last time we fired a shot so we can work out if were allowed to shoot
- void Update () {
- // If we press our left mouse button
- if (Input.GetButton("Fire1")) {
- // If the last time we fired is more than 0.15 seconds ago
- if (Time.time - lastFireTime > 0.15) {
- // Run the Fire function
- Fire();
- // Set our last fire time to now
- lastFireTime = Time.time;
- }
- }
- }
- void Fire () {
- // If our raycast is set off all the data about what was hit will be stored in this variable
- RaycastHit hit;
- // This creates our raycast at spawnPoint.transform.position which is at the end of our gun
- // The 2nd one is the direction, for this it will be forward of our rays spawnpoint
- // The 3rd one is how long will the ray be, in this case its infinity
- // This wont effect the performance so dont worry
- if (Physics.Raycast(spawnPoint.transform.position, spawnPoint.transform.forward, Mathf.Infinity, out hit)) {
- // Get the tag from what we collided with and if its "Player"
- if (hit.collider.gameObject.tag == "Player") {
- // How much damage to inflict
- int damageInt = 10;
- // This gets the gameObject of what we hit and sends a message to it
- // Telling it to run a function called removeHealth() and send it a value of damageInt
- // removeHealth a function of a script attached to your player
- hit.collider.gameObject.SendMessage("removeHealth", damageInt);
- }
- }
- }
- // This will be an example of the function receiving the damage
- // This will be in a script on player
- // This will expect an int from what ever is calling it
- void removeHealth (int i) {
- // Remove the health from your player
- myPlayerHealthVar -= i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement