Hey guys so I know there is threads on this but my situation is a bit different basically I am making a clone of the super crate box game and the user presses a button and bullet travells in that direction, problem is my condition in my if statement, I press the button and bullet goes in correct right but if I change my player looking position it will go in that direction. I can see why this is happening but don’t know how to fix it:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FireGun : MonoBehaviour
{
public List<GameObject> Guns = new List<GameObject>();
public PickupBx pbx;
private int count;
private string weaponFireType;
Vector2 leftOrRight;
//this will get the bullet prefab
public GameObject BulletPrefab;
//bullet position on player
public GameObject bulletPosition;
//the player
public GameObject player;
public GameObject bulletInScene;
void Start()
{
// bulletInScene = BulletPrefab;
pbx = GetComponent<PickupBx>();
}
// Update is called once per frame
void Update()
{
if (GetComponent<SpriteRenderer>().flipX == true)
{
leftOrRight = transform.TransformDirection(Vector2.left);
bulletPosition.transform.position = new Vector2(-92, 0);
}
else if (GetComponent<SpriteRenderer>().flipX == false)
{
leftOrRight = transform.TransformDirection(Vector2.right);
bulletPosition.transform.position = new Vector2(92, 0);
}
AddObjectToList(pbx.guns, Guns);
checkWeaponType();
}
//this method will loop through the gameobjects in pbxguns list (first parameter is the guns list in the pickupBx script), (myList is the list in this script)_
void AddObjectToList(List<GameObject> pbxGunsList, List<GameObject> myList)
{
//loop through all gaeobjects in the pbxGunsList list
foreach (GameObject go in pbxGunsList)
{
//if this list in this script doesn't contain the gameObjects from the other script
if (!myList.Contains(go))
{
//clear the list to make sure we don't get duplicates and have weapons in one 1 element
myList.Clear();
//add the gun to our list
myList.Add(go);
}
}
}
//check weapon the player is holding
void checkWeaponType()
{
//loop through the guns and set the string to weapon player is currently holding
for (int i = 0; i < Guns.Count; i++)
{
if (Guns[i].name == "weapons_0(Clone)")
{
weaponFireType = "MachineGun";
}
else if (Guns[i].name == "weapons_1(Clone)")
{
weaponFireType = "Shotgun";
}
else if (Guns[i].name == "weapons_2(Clone)")
{
weaponFireType = "RPG";
}
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
//make a new gameObject which will hold the bullet in the scene it will instantiate a bullet at the position of the bulletPosition empty gameObject.
bulletInScene = (GameObject)Instantiate(BulletPrefab, bulletPosition.transform.position, Quaternion.identity);
//the transform of the player
Transform cameraTransform = player.transform;
//Makes the player the parent of the GameObject currentGun
bulletInScene.transform.SetParent(cameraTransform, false);
bulletInScene.transform.Translate(Vector2.right * Time.deltaTime);
}
if (bulletInScene != null)
{
foreach (Transform t in transform)
{
if (t.name.Contains("Bullet(Clone)"))
{
transform.Find("Bullet(Clone)").transform.parent = null;
}
}
if (GetComponent<SpriteRenderer>().flipX == true)
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
bulletInScene.transform.Translate(Vector2.left * Time.deltaTime);
}
}
else if (GetComponent<SpriteRenderer>().flipX == false)
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
bulletInScene.transform.Translate(Vector2.right * Time.deltaTime);
}
}
}
}
}
This is the reason it does it, but don’t know what condition to put:
if (GetComponent<SpriteRenderer>().flipX == true)
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
bulletInScene.transform.Translate(Vector2.left * Time.deltaTime);
}
}
else if (GetComponent<SpriteRenderer>().flipX == false)
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
bulletInScene.transform.Translate(Vector2.right * Time.deltaTime);
}
}