public void Fire() { if (Time.time >= nextFire) { nextFire = Time.time + FireDelay + FireDelayModifier; int sign = 1; for (int i = 0; i < NumberOfShots + NumberOfShotsModifier; i++) { sign *= -1; ShotMover bullet = Instantiate(Shot, transform.position, transform.rotation).GetComponent <ShotMover>(); float correct = 0; if ((NumberOfShots + NumberOfShotsModifier) % 2 == 0) { correct = 7.5f; } bullet.transform.eulerAngles = new Vector3(0, sign * ((i + 1) / 2) * 15 - correct + transform.eulerAngles.y, 0); bullet.SetShooterTag(ShooterTag); bullet.Damage = Damage + DamageModifier; bullet.Speed = BulletSpeed + BulletSpeedModifier; } aud.Play(); } }
void OnTriggerEnter(Collider collider) { if (collider.tag.Equals("Bullet")) { ShotMover shotMover = collider.GetComponent <ShotMover>(); if (!shotMover.GetShooterTag().Equals("Player") && !m_Dead) { TakeDamage(shotMover.Damage); } } }
// Update is called once per frame void FixedUpdate() { float forceX = 0f; float forceY = 0f; float absVelX = Mathf.Abs (this._rigidBody2D.velocity.x); float absVelY = Mathf.Abs (this._rigidBody2D.velocity.y); this._movingValue = Input.GetAxis ("Horizontal"); //value is between -1 and 1 //check is player is moving if (this._movingValue != 0) { //we're moving this._animator.SetInteger("AnimState", 1); //play walk clip if(this._movingValue > 0) { //moving right if (absVelX < this.velocityRange.vMax){ forceX = this.speed; this._isFacingRight = true; this._flip(); } } else if (this._movingValue < 0) { //moving left if (absVelX < this.velocityRange.vMax){ forceX = -this.speed; this._isFacingRight = false; this._flip(); } } } else if (this._movingValue == 0) { //we're idle this._animator.SetInteger("AnimState", 0); } //Check if player is jumping if (Input.GetKey ("up") || Input.GetKey (KeyCode.W)) { //check if player is grounded if(this._isGrounded){ //player is jumping this._animator.SetInteger("AnimState", 3); if (absVelY < this.velocityRange.vMax) { forceY = this.jump; this._jumpSound.Play (); this._isGrounded = false; } } } this._rigidBody2D.AddForce (new Vector2 (forceX, forceY)); if (arrow != null) // if the playerObject gameObject-reference is not null - assigning the reference via FindWithTag at first frame - { Arrow = arrow.GetComponent<ShotMover>();// - set the PlayerController-reference (called playerControllerScript) to the <script component> of the Player gameobject (via the gameObject-reference) to have access the instance of the PlayerController script } if (arrow == null) //for exception handling - to have the console debug the absense of a player controller script in order for this entire code, the code in the GameController to work { Debug.Log("Cannot find SHOTMOVER"); } if (greenArrow) { if (Input.GetButton("Fire2") && Time.time > nextFire) //Hold key and shoot every 0.25 sec by - game time > nextFire = 0 { this._animator.SetInteger("AnimState", 4); nextFire = Time.time + fireRate; // then update nextFire = gametime (now 0.2) + fireRate (0.25) --> then when game time is 0.27 > nextFire (0.26) and fire button is held = shoot Bolt prefab via the reference shot gameObject Instantiate(arrow2, arrow2Spawn.position, arrow2.transform.rotation);//instantiate the game object shot per frame at a held key press, set at a vector3 position, at a set quaternion euler (rotation) //arrow.GetComponent<AudioSource>().playOnAwake = true;//upon instantiating the (shot), if the audio isn't playing on awake (on the very first frame), play this audio clip this._shoot.Play(); } } if (Input.GetButton("Fire1") && Time.time > nextFire) //Hold key and shoot every 0.25 sec by - game time > nextFire = 0 { this._animator.SetInteger("AnimState", 4); nextFire = Time.time + fireRate; // then update nextFire = gametime (now 0.2) + fireRate (0.25) --> then when game time is 0.27 > nextFire (0.26) and fire button is held = shoot Bolt prefab via the reference shot gameObject if(_isFacingRight) { Arrow.speed = 10; arrowSpawn.rotation = Quaternion.Euler(0, 180, 0); Instantiate(arrow, arrowSpawn.position, arrowSpawn.rotation);//instantiate the game object shot per frame at a held key press, set at a vector3 position, at a set quaternion euler (rotation) } else { Arrow.speed = -10; arrowSpawn.rotation = Quaternion.Euler(0, 0, 0); Instantiate(arrow, arrowSpawn.position, arrowSpawn.rotation);//instantiate the game object shot per frame at a held key press, set at a vector3 position, at a set quaternion euler (rotation) } //arrow.GetComponent<AudioSource>().playOnAwake = true;//upon instantiating the (shot), if the audio isn't playing on awake (on the very first frame), play this audio clip this._shoot.Play(); } }
// Update is called once per frame void FixedUpdate() { float forceX = 0f; float forceY = 0f; float absVelX = Mathf.Abs(this._rigidBody2D.velocity.x); float absVelY = Mathf.Abs(this._rigidBody2D.velocity.y); this._movingValue = Input.GetAxis("Horizontal"); //value is between -1 and 1 //check is player is moving if (this._movingValue != 0) { //we're moving this._animator.SetInteger("AnimState", 1); //play walk clip if (this._movingValue > 0) { //moving right if (absVelX < this.velocityRange.vMax) { forceX = this.speed; this._isFacingRight = true; this._flip(); } } else if (this._movingValue < 0) { //moving left if (absVelX < this.velocityRange.vMax) { forceX = -this.speed; this._isFacingRight = false; this._flip(); } } } else if (this._movingValue == 0) { //we're idle this._animator.SetInteger("AnimState", 0); } //Check if player is jumping if (Input.GetKey("up") || Input.GetKey(KeyCode.W)) { //check if player is grounded if (this._isGrounded) { //player is jumping this._animator.SetInteger("AnimState", 3); if (absVelY < this.velocityRange.vMax) { forceY = this.jump; this._jumpSound.Play(); this._isGrounded = false; } } } this._rigidBody2D.AddForce(new Vector2(forceX, forceY)); if (arrow != null) // if the playerObject gameObject-reference is not null - assigning the reference via FindWithTag at first frame - { Arrow = arrow.GetComponent <ShotMover>(); // - set the PlayerController-reference (called playerControllerScript) to the <script component> of the Player gameobject (via the gameObject-reference) to have access the instance of the PlayerController script } if (arrow == null) //for exception handling - to have the console debug the absense of a player controller script in order for this entire code, the code in the GameController to work { Debug.Log("Cannot find SHOTMOVER"); } if (greenArrow) { if (Input.GetButton("Fire2") && Time.time > nextFire) //Hold key and shoot every 0.25 sec by - game time > nextFire = 0 { this._animator.SetInteger("AnimState", 4); nextFire = Time.time + fireRate; // then update nextFire = gametime (now 0.2) + fireRate (0.25) --> then when game time is 0.27 > nextFire (0.26) and fire button is held = shoot Bolt prefab via the reference shot gameObject Instantiate(arrow2, arrow2Spawn.position, arrow2.transform.rotation); //instantiate the game object shot per frame at a held key press, set at a vector3 position, at a set quaternion euler (rotation) //arrow.GetComponent<AudioSource>().playOnAwake = true;//upon instantiating the (shot), if the audio isn't playing on awake (on the very first frame), play this audio clip this._shoot.Play(); } } if (Input.GetButton("Fire1") && Time.time > nextFire) //Hold key and shoot every 0.25 sec by - game time > nextFire = 0 { this._animator.SetInteger("AnimState", 4); nextFire = Time.time + fireRate; // then update nextFire = gametime (now 0.2) + fireRate (0.25) --> then when game time is 0.27 > nextFire (0.26) and fire button is held = shoot Bolt prefab via the reference shot gameObject if (_isFacingRight) { Arrow.speed = 10; arrowSpawn.rotation = Quaternion.Euler(0, 180, 0); Instantiate(arrow, arrowSpawn.position, arrowSpawn.rotation);//instantiate the game object shot per frame at a held key press, set at a vector3 position, at a set quaternion euler (rotation) } else { Arrow.speed = -10; arrowSpawn.rotation = Quaternion.Euler(0, 0, 0); Instantiate(arrow, arrowSpawn.position, arrowSpawn.rotation);//instantiate the game object shot per frame at a held key press, set at a vector3 position, at a set quaternion euler (rotation) } //arrow.GetComponent<AudioSource>().playOnAwake = true;//upon instantiating the (shot), if the audio isn't playing on awake (on the very first frame), play this audio clip this._shoot.Play(); } }