// Basically the Start method of the script, // since the Start of a base class script will not be called protected void InitShip() { if(m_exploder == null) m_exploder = Resources.Load("ShipPrefabs/ShipExplosion") as GameObject; m_thrust = GetComponent<ThrustScript>(); m_hitParticles = GetComponentInChildren<HitParticleSpawner>(); InitWeapons(); // Temporary Transform shieldTrans = transform.FindChild( "Shield" ); if( shieldTrans != null ) { m_shield = shieldTrans.GetComponent<ShieldScript>(); } }
// Basically the Start method of the script, // since the Start of a base class script will not be called protected void InitShip() { if (m_exploder == null) { m_exploder = Resources.Load("ShipPrefabs/ShipExplosion") as GameObject; } m_thrust = GetComponent <ThrustScript>(); m_hitParticles = GetComponentInChildren <HitParticleSpawner>(); InitWeapons(); // Temporary Transform shieldTrans = transform.FindChild("Shield"); if (shieldTrans != null) { m_shield = shieldTrans.GetComponent <ShieldScript>(); } }
// Update is called once per frame void Update() { // Check if player is looking at interactable RaycastHit interactHit; Ray shootRay = playerCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); if (Physics.Raycast(shootRay, out interactHit, interactRange, interactMask, QueryTriggerInteraction.Collide)) { Interactable target = interactHit.collider.GetComponent <Interactable>(); if (target != null) { target.Select(); if (Input.GetButtonDown("Interact")) { target.Interact(); } } } // Check if player is shooting if (Input.GetButtonDown("Fire")) { bool canShoot = gun.Fire(); RaycastHit gunHit; if (canShoot) { // Shooting disables visor visor.SetVisor(false); if (Physics.Raycast(shootRay, out gunHit, 100, shootMask, QueryTriggerInteraction.Collide)) { // Probably better to have some target component with an invokable event instead HitParticleSpawner particleSpawner = gunHit.collider.GetComponent <HitParticleSpawner>(); Enemy enemy = gunHit.collider.GetComponentInParent <Enemy>(); Destroyable destroyable = gunHit.collider.GetComponent <Destroyable>(); // Spawn particles if (particleSpawner != null) { particleSpawner.Shoot(gunHit, shootRay); } // Damage enemy if (enemy != null) { bool headshot = gunHit.collider.CompareTag("Head"); float damage = gun.damage; if (headshot) { damage *= gun.headshotBonus; } enemy.InflictDamage(damage); } // Destroy object if (destroyable != null) { destroyable.Shoot(gun.damage); } } } } }