Hi,
I am making a 2D platformer game (brand new at this whole thing!). My spirte flips left and right when moving, however I cannot get the projectile to shoot the same way, it always shoots right.
I have put all the code on the “player” so it shoots the projectile from there instead of putting the shooting mechanic on the bullet itself.
Here is my code for the player. Can anyone help me in saying what code needs to go where to make sure the bullet shoots the same way the player is facing?!?!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour
{
// Config
[Header(“Movement”)]
[SerializeField] float runSpeed = 5f;
[SerializeField] float jumpSpeed = 5f;
[Header(“Projectile”)]
[SerializeField] GameObject bulletPrefab;
[SerializeField] GameObject gun;
[SerializeField] float projectileSpeed = 10f;
[SerializeField] float projectileFiringPeriod = 0.1f;
[Header(“Player FX”)]
[SerializeField] AudioClip playerShoot;
[SerializeField] [Range(0, 1)] float playerShootVolume = 0.7f;
Coroutine shootingCoroutine;
// state
bool isAlive = true;
// Cached Component References
Rigidbody2D myRigidBody;
Animator myAnimator;
PolygonCollider2D myBodyCollider;
BoxCollider2D myFeet;
//Message then methods
void Start()
{
myRigidBody = GetComponent();
myAnimator = GetComponent();
myBodyCollider = GetComponent();
myFeet = GetComponent();
}
// Update is called once per frame
void Update()
{
if (!isAlive) { return; }
Run();
FlipSprite();
Jump();
Die();
Shoot();
}
private void Run()
{
float controlThrow = CrossPlatformInputManager.GetAxis(“Horizontal”); // -1 to +1
Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, myRigidBody.velocity.y);
myRigidBody.velocity = playerVelocity;
Debug.Log(playerVelocity);
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
myAnimator.SetBool(“Running”, playerHasHorizontalSpeed);
}
private void Jump()
{
if (!myFeet.IsTouchingLayers(LayerMask.GetMask(“Ground”))) { return; }
if (CrossPlatformInputManager.GetButtonDown(“Jump”))
{
Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
myRigidBody.velocity += jumpVelocityToAdd;
}
}
private void Die()
{
if (myBodyCollider.IsTouchingLayers(LayerMask.GetMask(“Enemy”, “Spikes”)))
{
isAlive = false;
myAnimator.SetTrigger(“Die”);
FindObjectOfType().ProcessPlayerDeath();
}
}
private void FlipSprite()
{
bool playerHasHorizontalSpeed = Mathf.Abs(myRigidBody.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
{
transform.localScale = new Vector2(Mathf.Sign(myRigidBody.velocity.x), 1f);
}
}
private void Shoot()
{
if (Input.GetButtonDown(“Fire1”))
{
shootingCoroutine = StartCoroutine(ShootContinuously());
}
if (Input.GetButtonUp(“Fire1”))
{
StopCoroutine(shootingCoroutine);
}
}
IEnumerator ShootContinuously()
{
while (true)
{
GameObject ball = Instantiate(
bulletPrefab,
gun.transform.position,
Quaternion.identity) as GameObject;
ball.GetComponent().velocity = new Vector2(projectileSpeed, 0);
yield return new WaitForSeconds(projectileFiringPeriod);
AudioSource.PlayClipAtPoint(playerShoot, Camera.main.transform.position, playerShootVolume);
}
}
}
A massive thanks in advance if someone can help me. As I say, I’m brand new to coding, so have 0 clues what I’m doing lol.