Pro_Unit

Crossbow

Mar 7th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Crossbow : MonoBehaviour
  6. {
  7.     public float offset;
  8.  
  9.     public GameObject ammo;
  10.     public Transform shotDir;
  11.  
  12.     private float timeShot;
  13.     public float startTime;
  14.  
  15.     // Start is called before the first frame update
  16.     void Start()
  17.     {
  18.        
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
  25.         float rotateZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
  26.         transform.rotation = Quaternion.Euler(0f, 0f, rotateZ + offset);
  27.  
  28.         if (timeShot <= 0)
  29.         {
  30.             if (Input.GetMouseButtonDown(0))
  31.             {
  32.                 Instantiate(ammo, shotDir.position, transform.rotation);
  33.                 timeShot = startTime;
  34.             }
  35.         }
  36.         else
  37.         {
  38.             timeShot -= Time.deltaTime;
  39.         }
  40.  
  41.    
  42.  
  43.        
  44.     }
Add Comment
Please, Sign In to add comment