- Home /
Player will only shoot left, not right
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class PlayerMove2D : MonoBehaviour {
Rigidbody2D rb2D;
Rigidbody2D projectile;
Vector3 mousePosition;
public AudioClip gunSnd;
public float turnSpeed = 4f;
public float moveSpeed = 5f;
public float strafeSpeed = 2f;
public float speed = 1.5f;
public bool lookRight;
// bullet stuff
public GameObject bulletProjectile;
public Transform bulletSpawn;
public float fireRate;
private float nextFire;
public float bulletSpeed;
private float step;
SpriteRenderer flashSprite;
// Use this for initialization
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
flashSprite = GameObject.Find("flash").GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
Fireball();
if (Input.GetKey(KeyCode.LeftArrow))
{
SetDirection(2);
transform.position += Vector3.left * speed * Time.deltaTime;
lookRight = false;
}
if (Input.GetKey(KeyCode.RightArrow))
{
SetDirection(-2);
transform.position += Vector3.right * speed * Time.deltaTime;
lookRight = true;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
}
void FixedUpdate()
{
// bullet shooting
if (Input.GetMouseButton(0) && (Time.time > nextFire))
{
nextFire = Time.time + fireRate;
Instantiate(bulletProjectile, bulletSpawn.position, bulletSpawn.rotation);
GetComponent<AudioSource>().PlayOneShot(gunSnd);
flashSprite.enabled = true;
StartCoroutine(WaitAndPrint(0.1f));
}
}
void Fireball()
{
if (Input.GetMouseButton(0) && (Time.time > nextFire))
{
if (lookRight == true)
{
Vector3 finalPosition = transform.position += Vector3.right * speed * Time.deltaTime;
nextFire = Time.time + fireRate;
Instantiate(bulletProjectile, finalPosition, transform.rotation);
GetComponent<AudioSource>().PlayOneShot(gunSnd);
flashSprite.enabled = true;
StartCoroutine(WaitAndPrint(0.1f));
}
else
{
Vector3 finalPosition = transform.position += Vector3.left * speed * Time.deltaTime;
nextFire = Time.time + fireRate;
Instantiate(bulletProjectile, finalPosition, transform.rotation);
GetComponent<AudioSource>().PlayOneShot(gunSnd);
flashSprite.enabled = true;
StartCoroutine(WaitAndPrint(0.1f));
}
}
}
void SetDirection(float direction)
{
Vector3 theScale = transform.localScale;
theScale.x = direction;
transform.localScale = theScale;
}
IEnumerator WaitAndPrint(float waitTime)
{
yield return new WaitForSeconds(waitTime);
flashSprite.enabled = false;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612051004im_/https://answers.unity.com/themes/thub/images/avi.jpg)