void Start() { bulletPools = new List <ObjectPool>(); impactPools = new List <ObjectPool>(); cloneSettings = GetComponent <LineRenderer>(); trailPool = new ObjectPool(createTrail()); cloneSettings.enabled = false; controller = GetComponent <GunController>(); foreach (GunHandler g in controller.gunInventory) { GunObject gun = g.gun; g.gameObject.SetActive(true); g.Initialize(); if (gun.shootType == GunObject.GunType.rigidbody && gun.rigidbodyBullet) { ObjectPool bulletPool = getFromPool(gun, gun.rigidbodyBullet, ref bulletPools); g.SetBulletPool(bulletPool.getPool); } if (gun.impactEffect == null) { continue; } ObjectPool impactPool = getFromPool(gun, gun.impactEffect, ref impactPools); g.SetImpactPool(impactPool.getPool); } if ((overlayAdjuster = GetComponentInParent <OverlayAdjuster>()) != null) { overlayAdjuster.GetScopes(); } }
void ApplyRecoil(float overTime) { GunObject gun = gunHandler.gun; float aimAdjust = (isAiming()) ? gun.aimDownMultiplier : 1f; Vector3 shotRecoil = Vector3.zero; if (gun.shooting == GunObject.ShootType.auto) { shotRecoil = gun.recoil.GetRecoil(continuousShots, gun.ammoClip, gun.cyclesInClip); } else if (gun.shooting == GunObject.ShootType.burst) { shotRecoil = gun.recoil.GetRecoil(continuousShots, gun.burstShot, 1); } else if (gun.shooting == GunObject.ShootType.semi) { shotRecoil = gun.recoil.GetRecoil(continuousShots, semiCalculations); } shotRecoil *= aimAdjust; cameraMovement.AddRecoil(shotRecoil, overTime); shotRecoil.y = 0; float z = shotRecoil.z; shotRecoil.x = gun.recoil.xRecoil.EvaluteValue(Random.Range(0.0f, 1.0f)); shotRecoil.x *= aimAdjust; shotRecoil *= overTime; shotRecoil.z = z; GunRecoil(shotRecoil, overTime); }
override protected void InitItem() { //_gun = GunObject.AddComponent<NWayGunItem>(); _gun = GunObject.AddComponent <SprialGunItem>(); _gun.SetBullet(BulletPrefab); _gun.InitGroupType(_groupType); }
Vector3 MuzzlePos() { GunObject currentGunObject = gunHolder.GetComponentInChildren <GunObject>(); Vector3 newPos = currentGunObject.muzzle.position; return(newPos); }
void CreateImpact(Vector3 pos, Vector3 normal) { GunObject gun = gunHandler.gun; if (gun.impactEffect == null || gunHandler.ImpactPool == null) { return; } PooledObject impact = gunHandler.ImpactPool.get(); impact.transform.position = pos; impact.transform.rotation = Quaternion.LookRotation(normal); if (!(impact as DamageObject)) { return; } DamageObject dmgImpact = (impact as DamageObject); dmgImpact.Damage = gun.bulletDamage; int simulation = dmgImpact.Simulate(); if (simulation < 0 || !ui) { return; } ui.ShowHitmarker(simulation > 0); //Damages and shows hitmarker }
void SpreadHandler() { if (gunHandler) { GunObject gun = gunHandler.gun; float actualSpread = bulletSpread; float spread = CurrentBulletSpread(); if (isAiming()) { spread *= gun.aimSpreadMultiplier; actualSpread = Mathf.Lerp(actualSpread, spread, Time.deltaTime * gun.aimDownSpeed * (1f - gun.aimDownMultiplier)); } else { actualSpread = Mathf.Lerp(actualSpread, spread, Time.deltaTime * 4f); } if (gunHandler.gun.canFireWhileDelayed) { float delaySpreadAdjust = (4f - (DelayPercent() * 3f)) / 4f; actualSpread = Mathf.Lerp(gunHandler.gun.bulletSpread, (spread * delaySpreadAdjust), DelayPercent()); } bulletSpread = actualSpread; } else { bulletSpread = 0.01f; } }
public void InitCharGunObject(string gunId) { if (charaData.IsDead || string.IsNullOrEmpty(gunId)) { return; } GunData gunData = GunDataTableObject.Instance.GetParams(gunId); _gunObject = ((GameObject)Instantiate((GameObject)Resources.Load("Prefabs/Items/Guns/" + gunData.GunPrefab))).GetComponent <GunObject>(); _gunObject.Owner = gameObject; //ソードを持ってる手のgameObjectを取得 if (weaponPosObj == null) { weaponPosObj = Utils.getChildGameObject(gameObject, "weapon"); } _gunObject.transform.SetParent(weaponPosObj.transform); _gunObject.transform.localPosition = Vector3.zero; //ポジションとレンダーオーダーを整える。 _gunObject.transform.localRotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, 90.0f)); _gunObject.GetComponent <SpriteRenderer> ().sortingLayerName = GetSortingLayerName(); _gunObject.GetComponent <SpriteRenderer> ().sortingOrder = 59; _gunObject.CopyParamsFrom(gunId); //gunをdisable _gunObject.DisplaySprite(false); }
private void OnEnable() { gun = (GunObject)target; prefabName = this.serializedObject.FindProperty("prefabName"); prefabObj = this.serializedObject.FindProperty("prefabObj"); prefabLocalData = this.serializedObject.FindProperty("prefabLocalData"); shootType = this.serializedObject.FindProperty("shootType"); rigidbodyBullet = this.serializedObject.FindProperty("rigidbodyBullet"); impactEffect = this.serializedObject.FindProperty("impactEffect"); initialForce = this.serializedObject.FindProperty("initialForce"); additiveForce = this.serializedObject.FindProperty("additiveForce"); muzzleFlash = this.serializedObject.FindProperty("muzzleFlash"); shooting = this.serializedObject.FindProperty("shooting"); firerate = this.serializedObject.FindProperty("firerate"); fireDelay = this.serializedObject.FindProperty("fireDelay"); fireCooldownSpeed = this.serializedObject.FindProperty("fireCooldownSpeed"); canFireWhileDelayed = this.serializedObject.FindProperty("canFireWhileDelayed"); bulletsPerShot = this.serializedObject.FindProperty("bulletsPerShot"); fireWhenPressedUp = this.serializedObject.FindProperty("fireWhenPressedUp"); burstShot = this.serializedObject.FindProperty("burstShot"); burstTime = this.serializedObject.FindProperty("burstTime"); ammoClip = this.serializedObject.FindProperty("ammoClip"); startingClips = this.serializedObject.FindProperty("startingClips"); looseAmmoOnReload = this.serializedObject.FindProperty("looseAmmoOnReload"); canFireWhileActing = this.serializedObject.FindProperty("canFireWhileActing"); bulletDamage = this.serializedObject.FindProperty("bulletDamage"); headshotMult = this.serializedObject.FindProperty("headshotMult"); bulletRange = this.serializedObject.FindProperty("bulletRange"); bulletSpread = this.serializedObject.FindProperty("bulletSpread"); aimSpreadMultiplier = this.serializedObject.FindProperty("aimSpreadMultiplier"); aimFOV = this.serializedObject.FindProperty("aimFOV"); aimDownSpeed = this.serializedObject.FindProperty("aimDownSpeed"); ironSightAim = this.serializedObject.FindProperty("ironSightAim"); aimDownMultiplier = this.serializedObject.FindProperty("aimDownMultiplier"); cyclesInClip = this.serializedObject.FindProperty("cyclesInClip"); recoil = this.serializedObject.FindProperty("recoil"); animationController = this.serializedObject.FindProperty("animationController"); gunMotions = this.serializedObject.FindProperty("gunMotions"); IK_HandTarget = this.serializedObject.FindProperty("IK_HandTarget"); gunIcon = this.serializedObject.FindProperty("gunIcon"); ammoOffsetX = this.serializedObject.FindProperty("ammoOffsetX"); shootClips = this.serializedObject.FindProperty("shootClips"); reloadSFX = this.serializedObject.FindProperty("reloadSFX"); }
void AdjustFOV(bool aiming) { GunObject curGun = SelectedGun().gun; float fov = SelectedGun().gun.aimFOV; animateFOV.SetFOV(aiming, curGun.aimFOV, curGun.aimDownSpeed); }
public static void ReturnGun(GunObject gunObj) { IEnumerator WaitForBulletsToReturn() { Debug.Log("Waiting For Bullets to Return before Returning"); Debug.Log("All Bullets have Returned"); Transform gun = ShootingController.currentGun.transform; gun.GetComponent <GunObject>().ammoPool.ReturnAllBullets(); while (gunObj.ammoPool.transform.childCount != gunObj.ammoPool.allBullets.Count) // while gunObj doesn't have all bullets { instance.CreatePickup(gunObj.gameObject); yield return(null); } gunObj.clipSize = gunObj.maxClipSize; gun.SetParent(GunManager.instance.transform); gun.gameObject.SetActive(false); instance.CreatePickup(gunObj.gameObject); yield return(null); } instance.StartCoroutine(WaitForBulletsToReturn()); }
void PlayShotSFX() { GunObject gun = gunHandler.gun; if (source == null || gun.shootClips.Length == 0) { return; } source.PlayOneShot(gun.GetRandomShootSFX()); }
// equip a gun public void EquipGun(GunObject gunObj) { DropGun(); AttachGunToPlayer(gunObj); currentGun = gunObj; hasGunEquipped = true; // for this project gunObj.StartCoroutine(gunObj.AutoShotRoutine()); }
ObjectPool getFromPool(GunObject gun, PooledObject target, ref List <ObjectPool> fromPool) { ObjectPool pool = null; if ((pool = fromPool.Find(x => (x.pooledObj.GetHashCode() == target.GetHashCode()))) == null) { pool = new ObjectPool(target); fromPool.Add(pool); } return(pool); }
void Start() { mainCam = Camera.main; GunUIManager.playerController = this.gameObject.transform.GetComponent <PlayerController>(); gunPoint = mainCam.transform.Find("Gun Point"); gunPrefabArea = gunPoint.Find("Gun Prefab Area"); if (useDefaultGun) { currentGun = defaultGun; } }
bool ReloadSelectedGun() { GunObject gun = gunHandler.gun; if (gun.reloadSFX != null) { source.clip = gun.reloadSFX; source.time = gun.reloadSFX.length * gunHandler.getReloadStartPoint(); source.Play(); } return(gunHandler.ReloadGun()); }
public void RefillAmmo() { foreach (GunHandler handler in gunInventory) { GunObject gun = handler.gun; if (gun.startingClips >= 0) { handler.totalAmmo = gun.ammoClip * gun.startingClips; } else { handler.ammoInClip = gun.ammoClip; } } }
void RigidbodyShot() { Transform camera = cameraMovement.transform; //Shoot from camera, not gun GunObject gun = gunHandler.gun; if (gun.rigidbodyBullet == null || gunHandler.BulletPool == null) { return; } Vector3 spawnPos = gunHandler.bulletSpawn.transform.position; Vector3 shotDir = getShotDir(bulletSpread); Vector3 worldDir = camera.TransformDirection(shotDir); //Get bullet from pool PooledObject bullet = gunHandler.BulletPool.get(); bullet.transform.position = spawnPos; bullet.transform.rotation = Quaternion.LookRotation(worldDir); if (bullet as DamageObject) { (bullet as DamageObject).Damage = gun.bulletDamage; } Rigidbody bulletBody = null; if ((bulletBody = bullet.transform.GetComponent <Rigidbody>()) == null) { return; } ShotHelper shotHelper = null; if ((shotHelper = bullet as ShotHelper) != null) { shotHelper.Initialize(bulletBody, gun, CreateImpact); } float force = gun.initialForce; if (gunHandler.gun.canFireWhileDelayed) { force *= DelayPercent(); } bulletBody.AddForce(worldDir * force, ForceMode.Impulse); }
public static AmmoPool CreateAmmoPool(GunObject parentObj) { Transform poolObj = new GameObject($"({parentObj.gunName}) Ammo Pool").transform; AmmoPool res = poolObj.gameObject.AddComponent(typeof(AmmoPool)) as AmmoPool; for (int i = 0; i < parentObj.ammoCount; i++) { GameObject bullet = Instantiate(parentObj.gunScriptableObj.bullet, poolObj); bullet.AddComponent <AmmoTag>(); res.GetComponent <AmmoPool>().allBullets.Add(bullet); bullet.SetActive(false); } return(res); }
//drops currently equipped gun public void DropGun() { Debug.Log(currentGun + " Before"); if (hasGunEquipped) { if (currentGun.ammoCount > 0) { GunManager.instance.CreatePickup(currentGun.gameObject); } GunManager.ReturnGun(currentGun); currentGun = null; hasGunEquipped = false; } Debug.Log(currentGun + " After"); }
public void Initialize(Rigidbody body, GunObject gun, UnityAction <Vector3, Vector3> impactCall) { Unpool(); gunShotFrom = gun; rigidbody = body; onImpact = impactCall; tr = GetComponentInChildren <TrailRenderer>(); StartCoroutine(changeLayer()); IEnumerator changeLayer() { TransformHelper.ChangeLayers(this.transform, "Overlay"); yield return(new WaitForSeconds(0.1f)); TransformHelper.ChangeLayers(this.transform, "Default"); } }
private void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.layer == 13) { Score += Random.Range(100, 300) * (God.Instance.Level + 1); God.Instance.Shaker.ShakeOnce(0.5f, 0.2f, 0.2f, 0.2f); } else if (other.gameObject.layer == 14) { Gun = other.GetComponent <GunPickable>().Gun; GunImage.sprite = Gun.DropTexture; GunSprite.sprite = Gun.Texture; GunImage.gameObject.SetActive(true); FireSlider.maxValue = Gun.GunTime; _gunTime = Gun.GunTime; } Destroy(other.gameObject); }
void SimulateShot() { GunObject gun = gunHandler.gun; if (gun.shootType == GunObject.GunType.raycast) { for (int i = 0; i < gun.bulletsPerShot; i++) { RaycastShot(); } } else { for (int i = 0; i < gun.bulletsPerShot; i++) { RigidbodyShot(); } } }
void RaycastShot() { Transform camera = cameraMovement.transform; //Shoot from camera, not gun GunObject gun = gunHandler.gun; Vector3 shotDir = getShotDir(bulletSpread); Vector3 worldDir = camera.TransformDirection(shotDir); Vector3 impactPos = camera.position; impactPos += worldDir * gun.bulletRange; Ray shotRay = new Ray(camera.position, worldDir); if (Physics.Raycast(shotRay, out var hit, gun.bulletRange, collisionLayer)) { DamageZone damaged = null; float dis = Vector3.Distance(camera.position, hit.point) + 0.05f; if (Physics.Raycast(shotRay, out var dmg, dis, damageLayer)) { damaged = dmg.transform.GetComponent <DamageZone>(); } impactPos = hit.point; CreateImpact(impactPos, hit.normal); if (damaged != null) //If we hit something we should damage { if (!damaged.DamageableAlreadyDead()) { bool killed = damaged.Damage(gun.bulletDamage, gun.headshotMult); if (ui) { ui.ShowHitmarker(killed); //Damages and shows hitmarker } } } } Vector3[] pos = { gunHandler.bulletSpawn.transform.position, impactPos }; ShotTrailHelper trail = helper.getAvailableTrail(); trail.Initialize(pos); }
private void Update() { FireSlider.value = _gunTime; _gunTime = Mathf.Max(0, _gunTime - Time.deltaTime); if (_gunTime == 0) { Gun = God.Instance.DefaultGun; GunImage.gameObject.SetActive(false); } ScoreText.text = "SCORE: " + Score; _tick += Time.deltaTime; if (_tick >= PathDelay) { _tick = 0; Bfs.Search(Walls, new Vector2Int((int)transform.position.x, (int)transform.position.y)); } if (God.Instance.UseMouse) { //transform.LookAt2D(God.Instance.Camera.ScreenToWorldPoint(Input.mousePosition)); GunSprite.transform.LookAt2D(God.Instance.Camera.ScreenToWorldPoint(Input.mousePosition)); } Vector2 moving = new Vector2(Input.GetAxis(_horAxis), Input.GetAxis(_verAxis)); if (moving.sqrMagnitude > 0.01f) { Animator.speed = 1f; _direction = moving.normalized; transform.LookAt2D((Vector2)transform.position + _direction); _rigidbody.MovePosition((Vector2)transform.position + moving.normalized * God.Instance.PlayerSpeed * Time.deltaTime); } else { Animator.speed = 0.1f; } _fireCoolDown -= Time.deltaTime; if (_fireCoolDown < 0 && Input.GetAxis(_fireAxis) > 0) { _fireCoolDown = Gun.Delay * God.Instance.FireCoolDown; Fire(_direction); } }
public void ChangeGunObject(GunObject theGun) { GameObject oldGun = gunHolder.GetComponentInChildren <GunObject>().gameObject; if (oldGun != null) { Destroy(oldGun); } if (MousePosition().x > transform.position.x) { GameObject newGun = Instantiate(theGun.gameObject, gunHolder.position, gunHolder.rotation) as GameObject; newGun.transform.parent = gunHolder; } else if (MousePosition().x < transform.position.x) { GameObject newGun = Instantiate(theGun.gameObject, gunHolder.position, gunHolder.rotation) as GameObject; newGun.transform.localScale = new Vector3(1, -1, 1); newGun.transform.parent = gunHolder; } }
void SpreadHandler() { if (gunHandler) { GunObject gun = gunHandler.gun; float spread = CurrentBulletSpread(); if (isAiming()) { spread *= gun.aimSpreadMultiplier; bulletSpread = Mathf.Lerp(bulletSpread, spread, Time.deltaTime * gun.aimDownSpeed * (1f - gun.aimDownMultiplier)); } else { bulletSpread = Mathf.Lerp(bulletSpread, spread, Time.deltaTime * 4f); } } else { bulletSpread = 0.01f; } }
public void InitializeGun(GunHandler handler) { GunObject gun = handler.gun; handler.gameObject.SetActive(true); handler.Initialize(); if (gun.shootType == GunObject.GunType.rigidbody && gun.rigidbodyBullet) { ObjectPool bulletPool = getFromPool(gun, gun.rigidbodyBullet, ref bulletPools); handler.SetBulletPool(bulletPool.getPool); } if (gun.impactEffect == null) { return; } ObjectPool impactPool = getFromPool(gun, gun.impactEffect, ref impactPools); handler.SetImpactPool(impactPool.getPool); }
public void Attack() { //Swordオブジェクトはキャラに既に付いているのでNullチェックだけでは装備してるかわからない。 //Owner、SwingEffectをチェック if (this.SwordObject != null && SwordObject.SwingEffectPrefab != null && this.SwordObject.CanReachEnemy()) { SwordObject.DisplaySprite(true); if (GunObject != null) { GunObject.DisplaySprite(false); } SwordObject.Slash(); } else if (this.GunObject != null) { if (SwordObject != null) { SwordObject.DisplaySprite(false); } GunObject.DisplaySprite(true); GunObject.Fire(); } }
// Item protected override void InitItem() { _gun = GunObject.AddComponent <GunItem>(); _gun.SetBullet(BulletPrefab); }
public void AddGunTemporarily(GunObject addGun) //Will not change the prefab, this should be called if the game is running { RemoveBlanks(); //Find where to place this gun GunHandler handler = gunInventory.Find(x => x.gun.GetHashCode() == addGun.GetHashCode()); if (handler == null) //If we did not find a handler with the gun using the gun name { GameObject gunParent = new GameObject(); gunParent.transform.name = addGun.prefabName; gunParent.transform.SetParent(this.transform); TransformHelper.ResetLocalTransform(gunParent.transform); Animator ani = gunParent.AddComponent(typeof(Animator)) as Animator; if (addGun.animationController != null) { ani.runtimeAnimatorController = addGun.animationController; } handler = gunParent.AddComponent(typeof(GunHandler)) as GunHandler; handler.gun = addGun; handler.handIKTarget = new GameObject().transform; handler.handIKTarget.name = "IK_Hand"; handler.handIKTarget.SetParent(gunParent.transform); TransformHelper.SetLocalTransformData(handler.handIKTarget, addGun.IK_HandTarget); gunInventory.Add(handler); handler.gunIndex = gunInventory.Count - 1; } else { Debug.Log("Already have gun named : " + addGun.prefabName + " [UPDATING GUN]"); handler.transform.SetParent(this.transform); TransformHelper.ResetLocalTransform(handler.transform); if (addGun.animationController != null) { Animator ani = handler.gameObject.GetComponent <Animator>(); ani.runtimeAnimatorController = addGun.animationController; } handler.gun = addGun; handler.handIKTarget.SetParent(handler.transform.parent); TransformHelper.DeleteAllChildren(handler.transform); handler.handIKTarget.SetParent(handler.transform); TransformHelper.SetLocalTransformData(handler.handIKTarget, addGun.IK_HandTarget); } if (addGun.prefabObj != null) { CreateGunPrefab(handler); } if (addGun.animationController != null) { handler.SetAnimations(addGun.gunMotions); } handler.SetAmmo(); handler.bulletSpawn = handler.gameObject.GetComponentInChildren <GunBulletSpawn>(); helper.InitializeGun(handler); handler.gameObject.SetActive(false); //Swap to new gun putAwayGun = selectedGun; source.Stop(); //Stop reloading SFX if playing fireDelayTimer = 0; //Reset the fire delay SelectedGun().PutAwayWeapon(() => TakeOutSelectedGun()); selectedGun = handler.gunIndex; status = GunControlStatus.swapping; }