Why won't this work!?

So my game is a 2D platformer where you can flip the world upside down. Objects flip upside down including the camera and gravity is reversed. I’m trying to make the basic enemies, when upside down, shoot at the player when they are infront of it but it shoots when the player is behind it instead. Why is it doing this!?

#pragma strict


var playerRange : float;

var enemyBullet : GameObject;

var player : GameObject;

var launchPoint : Transform;

var waitBetweenShots : float;

var anim : Animator;

private var shotCounter : float;
function Start () {
player = GameObject.FindWithTag ("Player");
anim = GetComponent(Animator);
shotCounter = waitBetweenShots;
}

function Update () {

shotCounter -= Time.deltaTime;

Debug.DrawLine ( new Vector3(transform.position.x - playerRange,transform.position.y,transform.position.z),new Vector3(transform.position.x + playerRange,transform.position.y,transform.position.z));
if(GravityChange.upsideDown == false) {

if(transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange && shotCounter < 0)
{

anim.Play("dogShoot");
shotCounter = waitBetweenShots;
Instantiate(enemyBullet,launchPoint.position,Quaternion.identity);
}

if(transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x - playerRange && shotCounter < 0)
{
anim.Play("dogShoot");
shotCounter = waitBetweenShots;
Instantiate(enemyBullet,launchPoint.position,Quaternion.identity);
}
}
//upside down
if(GravityChange.upsideDown == true) {

if(transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x - playerRange && shotCounter < 0)
{

anim.Play("dogShoot");
shotCounter = waitBetweenShots;
Instantiate(enemyBullet,launchPoint.position,Quaternion.identity);
}

if(transform.localScale.x > 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange && shotCounter < 0)
{
anim.Play("dogShoot");
shotCounter = waitBetweenShots;
Instantiate(enemyBullet,launchPoint.position,Quaternion.identity);
}
}


}

Edit I tried then failed to understand whats going on here so i deleted my post GL.

What don’t you understand?

Silverriolu295:

Hi,

You say you flip the objects, world and camera, but I guess with an exception of flipping player?

Either way, can you show how you actually “flip” the level? Do you rotate it or scale it?

Check it in the editor because if you are flipping everything including the camera then what you think is a position < may actually be > in world space & vice versa. I.e. It may look to be on the right of the screen but when it is flipped around the right way it is actually on the left of the screen.

No, I should’ve made this clear. Important objects like the player, enemies and the camera flip. The ground doesn’t move.
Here is a visual: http://imgur.com/WHFjTxW

The enemy moves correctly and bullets go the right direction when dropped in as a prefab, but the enemy doesn’t shoot them correctly. When the player is behind the enemy, he shoots the opposite direction of the player. When the enemy is facing the player, he doesn’t shoot at all!

Just to make sure of it, don’t scale your camera negatively. (You should never scale your camera at all actually.)
If you do scale the camera negatively, that would explain a lot as even if what you see is flipped, the world itself isn’t so if the enemy should shoot on the positive X axis, when the world flip, they will keep shooting on the positive axis even if you flipped them.

You got 2 possibilities :
A) Rotate the ground (world) 180 degrees instead of flipping everything. To make it rotate properly and to keep the player at it’s current position, you will have to make the world (and everything included) move instead of the player or camera. If the rotation is done through the middle of the screen, you will have to make sure the world’s center us always in the center of the camera. Same if it’s rotating around the player.

From the visual you gave us, that’s what I see as a solution.

B) If you really want to keep the flipping of the stuff (which will add more and more problems on the road whenever you have things working based on the world’s X axis.), the correction to the current script is the orientation of the enemies’ bullet which instantiate while being oriented at Quaternion.identity both when it’s inversed and not. If you made those to fly automatically “forward”, then it will move “forward” in the world which doesn’t change when flipped.
While I’m not sure it will work, maybe something like Quaternion.Inverse() will work when the world is flipped.