My Shooting Script! (demo)


using UnityEngine;
using System.Collections;
[AddComponentMenu("Playground/Gameplay/Object Shooter")]
public class ObjectShooter : MonoBehaviour
{
    public GameObject hand;
    private slots handslot;
    [Header("Object creation")]
    private ammoweapon ammo;
 public GameObject prefabToSpawn;
    private float reloadtime = 0f;
    public float timetoraload = 2f;
    private bool reload = false;
    private bool isreloading = false;
    public int startamountofaammo = 30;
public KeyCode keyToPress = KeyCode.Space;
[Header("Other options")]
public float creationRate = .5f;
public float shootSpeed = 5f;
public Vector2 shootDirection = new Vector2(1f, 1f);
    public float rozrzut = 0;
public bool relativeToRotation = true;
private float timeOfLastSpawn;
private int playerNumber;
// Use this for initialization
 void Start ()
 {
        handslot = GameObject.FindGameObjectWithTag("hand").GetComponent <slots>();
        ammo = GameObject.FindGameObjectWithTag("Player").GetComponent<ammoweapon>();
        ammo.ammo = startamountofaammo;
 timeOfLastSpawn = -creationRate;
// Set the player number based on the GameObject tag
 playerNumber = (gameObject.CompareTag("Player")) ? 0 : 1;
 }
// Update is called once per frame
 void Update ()
 {
        shootDirection = new Vector2(Random.Range(-rozrzut, rozrzut), 1f);
        if (reloadtime <= 0f && isreloading == true)
        {
            ammo.ammo = startamountofaammo;
            reload = false;
            isreloading = false;
            ammo.reloadtext.text = "";
            ammo.reloadimage.color = ammo.transparents;
        }
        if(reloadtime > 0f)
        {
            ammo.reloadtext.text = "rloading " + reloadtime;
            reloadtime -= Time.deltaTime;
            reload = true;
            ammo.reloadimage.color = ammo.colorreload;
        }
        if(ammo.ammo <= 0 && isreloading == false)
        {
            isreloading = true;
            reload = true;
            reloadtime = timetoraload;
            ammo.panim.SetTrigger("re");
        }
        if(ammo.wanttoreload == true)
        {
            ammo.wanttoreload = false;
            isreloading = true;
            reload = true;
            reloadtime = timetoraload;
            ammo.panim.SetTrigger("re");
        }
 if(Input.GetKey(keyToPress)
    && Time.time >= timeOfLastSpawn + creationRate && reload == false)
 {
 Vector2 actualBulletDirection = (relativeToRotation) ? (Vector2)(Quaternion.Euler(0, 0, transform.eulerAngles.z) * shootDirection) : shootDirection;
            ammo.ammo -= 1;
 GameObject newObject = Instantiate<GameObject>(prefabToSpawn);
 newObject.transform.position = this.transform.position;
 newObject.transform.eulerAngles = new Vector3(0f, 0f, Utils.Angle(actualBulletDirection));
 newObject.tag = "Bullet";
// push the created objects, but only if they have a Rigidbody2D
 Rigidbody2D rigidbody2D = newObject.GetComponent<Rigidbody2D>();
 if(rigidbody2D != null)
 {
 rigidbody2D.AddForce(actualBulletDirection * shootSpeed, ForceMode2D.Impulse);
 }
BulletAttribute b = newObject.GetComponent<BulletAttribute>();
 if(b == null)
 {
 b = newObject.AddComponent<BulletAttribute>();
 }
 b.playerId = playerNumber;
timeOfLastSpawn = Time.time;
 }
 }
void OnDrawGizmosSelected()
 {
 if(this.enabled)
 {
 float extraAngle = (relativeToRotation) ? transform.rotation.eulerAngles.z : 0f;
 Utils.DrawShootArrowGizmo(transform.position, shootDirection, extraAngle, 1f);
 }
 }
    public void reloading()
    {
        ammo.ammo = 0;
    }
}
//AND AMMO SCRIPT:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
public class ammoweapon : MonoBehaviour
{
    public int ammo = 30;
    public TMP_Text ammotext;
    public TMP_Text reloadtext;
    public Color colorreload;
    public Color transparents;
    public Image reloadimage;
    public Animator panim;
    public bool wanttoreload = false;
    // Start is called before the first frame update
    void Start()
    {
        ammotext.text = ("" + ammo);
    }
    // Update is called once per frame
    void Update()
    {
        ammotext.text = "" + ammo;
    }
    public void Reload()
    {
        wanttoreload = true;
    }
}

Get JUSTSHOOT.lol

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.