Advertisement
Guest User

RayCastShooting.js

a guest
Nov 24th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. var TheDamage : int;
  2. var hitParticle : Transform;
  3. var Gun : GameObject;
  4. var AnimationName : String;
  5. //~~~~
  6. var CurrentAmmo : int;
  7. var Magazines : int;
  8. var MaxAmmo : int;
  9. var MaxMagazines : int;
  10. var CanShoot = true;
  11. var ReloadAnimationName : String;
  12. var style : GUIStyle;
  13.  
  14. function Start () {
  15.  
  16. }
  17.  
  18. function Update () {
  19. if(CurrentAmmo <= 0 && Magazines >0){
  20. CanShoot = false;
  21. CurrentAmmo = MaxAmmo;
  22. Reload();
  23. }
  24.  
  25. if(Input.GetButton("Fire1") && CurrentAmmo > 0 && CanShoot){
  26. networkView.RPC("Shoot", RPCMode.All);
  27. Gun.animation.Play(AnimationName);
  28. CurrentAmmo -= 1;
  29. }
  30.  
  31. }
  32.  
  33. @RPC
  34. function Shoot() {
  35. if(networkView.isMine){
  36. var hit : RaycastHit;
  37. var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5,Screen.height*0.5,0));
  38. if(Physics.Raycast (ray, hit, 100)){
  39. var particleClone = Network.Instantiate(hitParticle, hit.point, Quaternion.LookRotation(hit.normal), 0);
  40. Destroy(particleClone.gameObject, 0.5);
  41. if(hit.transform.tag == "Player"){
  42. hit.transform.networkView.RPC("ApplyDamage", RPCMode.All, TheDamage);
  43.  
  44. }
  45. }
  46. }
  47. }
  48.  
  49. function Reload() {
  50. GetComponentInChildren(Recoil).enabled = false;
  51. Gun.animation.Play(ReloadAnimationName);
  52. Magazines -= 1;
  53. yield WaitForSeconds(2);
  54. CanShoot = true;
  55. GetComponentInChildren(Recoil).enabled = true;
  56. }
  57.  
  58. function OnGUI() {
  59. GUILayout.Label("Ammo: "+CurrentAmmo+"/"+Magazines,style);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement