Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class pistol : MonoBehaviour {
- public float shootdelay = 0, damage = 20;
- public LayerMask whattohit;
- public Transform firepoint;
- public Transform bullettrailprefab;
- // Use this for initialization
- void Start () {
- firepoint = transform.Find("shootpoint");
- }
- // Update is called once per frame
- void Update () {
- shootdelay += Time.deltaTime;
- if (shootdelay >= 0.5f)
- {
- if (Input.GetKey(KeyCode.Mouse0))
- {
- shootdelay = 0;
- shot();
- }
- }
- }
- public void shot()
- {
- Vector2 mousepos = new Vector2
- (Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
- Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
- Vector2 firepointpos = new Vector2(firepoint.position.x, firepoint.position.y);
- effect();
- RaycastHit2D hit = Physics2D.Raycast
- (firepointpos, (mousepos - firepointpos), 10f, whattohit);
- Debug.DrawLine(firepointpos, (mousepos - firepointpos) * 100, Color.cyan);
- if (hit.collider != null)
- {
- Debug.DrawLine(firepointpos, hit.point, Color.red);
- Debug.Log("We hit " + hit.collider.name);
- hit.collider.SendMessageUpwards("Damage", damage);
- }
- }
- void effect()
- {
- Transform clone = Instantiate(bullettrailprefab, firepoint.position, firepoint.rotation);
- clone.parent = firepoint;
- Destroy(clone.gameObject, 0.1f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement