Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PlayerAttack.js
- #pragma strict
- var hitDistance : float = 1.5;
- var attackStrength = 50.0; //damage dealt to zombie
- var attackForce = 800.0; //determines the knockback on a zombie
- var attackCounterSet = 10;
- var attackCounter = attackCounterSet;
- function FixedUpdate () {
- if(attackCounter > 0){
- attackCounter--;
- }
- if(Input.GetMouseButtonUp(0) && attackCounter <= 0){ //if left click and timer is 0, attack
- attackCounter = attackCounterSet;
- Attack();
- }
- }
- function Attack(){
- var cam : Camera = Camera.main;
- var ray : Ray = cam.ScreenPointToRay(Vector3(cam.pixelWidth /2, cam.pixelHeight / 2, 0));
- var hit : RaycastHit;
- if (Physics.Raycast(ray, hit)){
- if((hit.collider.gameObject.tag == "Zombie") && (hit.distance <= hitDistance)){ //if distance within range && is zombie
- hit.collider.GetComponent(ZombieScript).adjustHealth(attackStrength); //adjustHealth
- hit.rigidbody.AddRelativeForce(0, 0, -attackForce); //force back
- if(hit.collider.GetComponent(ZombieScript).health <=0){
- //die code
- Destroy(hit.collider.gameObject);
- }
- }
- }
- }
- function OnGUI(){
- GUI.Box(new Rect (Camera.main.pixelWidth/2-25, Camera.main.pixelHeight/2-25, 50, 50), "X");
- }
Advertisement
Add Comment
Please, Sign In to add comment