public virtual GameObject Shoot(Vector2 shootDirection) { GameObject projectile = CreateProjectile(shootDirection); BulletScript bulletScript = projectile.GetComponent <BulletScript>(); rb.AddForce(-shootDirection * kickbackForceMplier * rb.mass * Time.fixedDeltaTime, ForceMode2D.Impulse); return(projectile); }
/// <summary> /// creates a shot and sets it to the target's direction, if it's a missile, sets it to follow the target. /// Returns the projectile /// </summary> /// <param name="target"></param> /// <returns></returns> public GameObject Shoot(Transform target) { GameObject projectile = CreateProjectile(target.transform.position - transform.position); // Set bullet damage BulletScript bulletScript = projectile.GetComponent <BulletScript>(); Missile missile = bulletScript as Missile; if (missile != null) { missile.Target = target; } return(projectile); }
private GameObject CreateProjectile(Vector2 shootDirection) { shootDirection.Normalize(); float randomOffset = Random.Range(-CurrentWeaponStats.wiggleShootOffset, CurrentWeaponStats.wiggleShootOffset); Vector2 positionWithWiggle = new Vector2(-shootDirection.y, shootDirection.x).normalized *randomOffset; Vector2 shootPosition = (Vector2)shootTransform.position + positionWithWiggle; if (audioSource) { audioSource.PlayOneShot(CurrentWeaponStats.shootSound, Random.Range(0.7f, 1.0f)); } else { Debug.LogError("Audio source is not assigned!!"); } // Create simple rotation which looks where the player is aiming in addition to a wiggle amount of euler angles // How? IDK, just leave it, it works float randomRotation = Random.Range(-CurrentWeaponStats.randomShootAngle, CurrentWeaponStats.randomShootAngle); Quaternion bulletRotation = Quaternion.LookRotation(Quaternion.Euler(0, 0, randomRotation) * shootDirection); GameObject projectile = Instantiate(CurrentWeaponStats.projectilePrefab, shootPosition, bulletRotation); // Set projectile movement Rigidbody2D projectileRigidbody2D = projectile.GetComponent <Rigidbody2D>(); projectileRigidbody2D.velocity = Quaternion.Euler(0, 0, randomRotation) * shootDirection * CurrentWeaponStats.projectileSpeed; BulletScript bulletScript = projectile.GetComponent <BulletScript>(); bulletScript.DamageAmount = Mathf.RoundToInt(CurrentWeaponStats.damage * Random.Range(0.8f, 1.2f)); bulletScript.Shooter = this.gameObject; return(projectile); }