protected void SpawnBullet() { //we check if there is a wall between the player and the bullet spawn position, if there is, we don't spawn a bullet //otherwise, the player can "shoot throught wall" because the arm extend to the other side of the wall Vector2 testPosition = transform.position; testPosition.y = m_CurrentBulletSpawnPoint.position.y; Vector2 direction = (Vector2)m_CurrentBulletSpawnPoint.position - testPosition; float distance = direction.magnitude; direction.Normalize(); RaycastHit2D[] results = new RaycastHit2D[12]; if (Physics2D.Raycast(testPosition, direction, m_CharacterController2D.ContactFilter, results, distance) > 0) { return; } BulletObject bullet = bulletPool.Pop(m_CurrentBulletSpawnPoint.position); bool facingLeft = m_CurrentBulletSpawnPoint == facingLeftBulletSpawnPoint; bullet.rigidbody2D.velocity = new Vector2(facingLeft ? -bulletSpeed : bulletSpeed, 0f); bullet.spriteRenderer.flipX = facingLeft ^ bullet.bullet.spriteOriginallyFacesLeft; rangedAttackAudioPlayer.PlayRandomSound(); }
public void Shooting() { Vector2 shootPosition = shootingOrigin.transform.localPosition; //if we are flipped compared to normal, we need to localy flip the shootposition too if ((spriteFaceLeft && m_SpriteForward.x > 0) || (!spriteFaceLeft && m_SpriteForward.x > 0)) { shootPosition.x *= -1; } BulletObject obj = m_BulletPool.Pop(shootingOrigin.TransformPoint(shootPosition)); shootingAudio.PlayRandomSound(); obj.rigidbody2D.velocity = (GetProjectilVelocity(m_TargetShootPosition, shootingOrigin.transform.position)); }
public void Shooting() { var force = m_SpriteForward.x > 0 ? Vector2.right.Rotate(shootAngle) : Vector2.left.Rotate(-shootAngle); force *= shootForce; Vector2 shootPosition = shootingOrigin.transform.localPosition; //if we are flipped compared to normal, we need to localy flip the shootposition too if (spriteFaceLeft && m_SpriteForward.x > 0 || !spriteFaceLeft && m_SpriteForward.x > 0) { shootPosition.x *= -1; } var obj = m_BulletPool.Pop(shootingOrigin.TransformPoint(shootPosition)); shootingAudio.PlayRandomSound(); obj.rigidbody2D.velocity = GetProjectilVelocity(m_TargetShootPosition, shootingOrigin.transform.position); }
protected void SpawnBullet() { //we check if there is a wall between the player and the bullet spawn position, if there is, we don't spawn a bullet //otherwise, the player can "shoot throught wall" because the arm extend to the other side of the wall Vector2 testPosition = transform.position; testPosition.y = m_CurrentBulletSpawnPoint.position.y; Vector2 direction = (Vector2)m_CurrentBulletSpawnPoint.position - testPosition; float distance = direction.magnitude; direction.Normalize(); RaycastHit2D[] results = new RaycastHit2D[12]; if (Physics2D.Raycast(testPosition, direction, m_CharacterController2D.ContactFilter, results, distance) > 0) { return; } BulletObject bullet = bulletPool.Pop(m_CurrentBulletSpawnPoint.position); Vector2 v = new Vector2(Mathf.Cos(arm.eulerAngles.z * Mathf.Deg2Rad), Mathf.Sin(arm.eulerAngles.z * Mathf.Deg2Rad)); bullet.rigidbody2D.velocity = bulletSpeed * v; }