Advertisement
CakeMeister

2dshooter pistoleffect phan 5

Jul 26th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class pistol : MonoBehaviour {
  6.     public float shootdelay = 0, damage = 20;
  7.     public LayerMask whattohit;
  8.     public Transform firepoint;
  9.  
  10.     public Transform bullettrailprefab;
  11.     // Use this for initialization
  12.     void Start () {
  13.        
  14.         firepoint = transform.Find("shootpoint");
  15.     }
  16.    
  17.     // Update is called once per frame
  18.     void Update () {
  19.         shootdelay += Time.deltaTime;
  20.        
  21.         if (shootdelay >= 0.5f)
  22.         {
  23.             if (Input.GetKey(KeyCode.Mouse0))
  24.             {
  25.                 shootdelay = 0;
  26.                 shot();
  27.             }
  28.         }
  29.     }
  30.  
  31.     public void shot()
  32.     {
  33.         Vector2 mousepos = new Vector2
  34.             (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
  35.              Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
  36.  
  37.         Vector2 firepointpos = new Vector2(firepoint.position.x, firepoint.position.y);
  38.         effect();
  39.         RaycastHit2D hit = Physics2D.Raycast
  40.             (firepointpos, (mousepos - firepointpos), 10f, whattohit);
  41.         Debug.DrawLine(firepointpos, (mousepos - firepointpos) * 100, Color.cyan);
  42.  
  43.         if (hit.collider != null)
  44.         {
  45.             Debug.DrawLine(firepointpos, hit.point, Color.red);
  46.             Debug.Log("We hit " + hit.collider.name);
  47.             hit.collider.SendMessageUpwards("Damage", damage);
  48.         }
  49.            
  50.          
  51.     }
  52.    
  53.     void effect()
  54.     {
  55.         Transform clone = Instantiate(bullettrailprefab, firepoint.position, firepoint.rotation);
  56.         clone.parent = firepoint;
  57.         Destroy(clone.gameObject, 0.1f);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement