View difference between Paste ID: i1cViLAJ and LBvVFd7K
SHOW: | | - or go back to the newest paste.
1
//PlayerAttack.js
2
#pragma strict
3
4
var hitDistance : float = 1.5;
5
6
var attackStrength = 50.0;  //damage dealt to zombie
7
var attackForce = 800.0;    //determines the knockback on a zombie
8
var attackCounterSet = 10;
9
var attackCounter = attackCounterSet;
10
11
function FixedUpdate () {
12
	if(attackCounter > 0){
13
		attackCounter--;
14
	}
15
	
16
	if(Input.GetMouseButtonUp(0) && attackCounter <= 0){   //if left click and timer is 0, attack
17
		attackCounter = attackCounterSet;
18
		Attack();
19
		
20
	}
21
}
22
23
function Attack(){
24
25
	
26
	
27
	var cam : Camera = Camera.main;
28
	var ray : Ray = cam.ScreenPointToRay(Vector3(cam.pixelWidth /2, cam.pixelHeight / 2, 0));
29
	var hit : RaycastHit;
30
	if (Physics.Raycast(ray, hit)){
31
	
32
  		if((hit.collider.gameObject.tag == "Zombie") && (hit.distance <= hitDistance)){  //if distance within range && is zombie
33
       	  hit.collider.GetComponent(ZombieScript).adjustHealth(attackStrength);     //adjustHealth
34
      	  hit.rigidbody.AddRelativeForce(0, 0, -attackForce);   //force back
35
      	  
36
       		if(hit.collider.GetComponent(ZombieScript).health <=0){
37
        	//die code
38
        	Destroy(hit.collider.gameObject);
39
       
40
            }                    
41
        }
42
        
43
        
44
    }
45
	
46
	
47
	
48
}
49
50
function OnGUI(){
51
GUI.Box(new Rect (Camera.main.pixelWidth/2-25, Camera.main.pixelHeight/2-25, 50, 50), "X");
52
}