Unity is ignoring the minus

I have been trying to get my sidescroller shooter character to turn aside when the rotations z is lower than -0.4 and turn to the other side when its higer than -0.4 so the character wont shoot one direction while facing the legs to the other. ( Simply shooting from between its legs (XD).)
Most of the times the character turns when it reaches -0.4 but in some cases (randomly yet) it just ignors the minus (while the z shows it should turn and gets the charactars head between the legs.

Here is the piece of code wich gets it to turn:

mousepos=Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,2));
		mousepos.x=0;
		if(hands.transform.rotation.z<-0.4)
		{
			facefor=false;
			me.transform.rotation=new Quaternion(0,0,0,0);
			hands.LookAt(mousepos,Vector3.right);
		}
		if(hands.transform.rotation.z>-0.4)
		{
			facefor=true;
			me.transform.rotation=new Quaternion(0,-180,0,0);
			hands.LookAt(mousepos,Vector3.left);
		}

The whole things looks odd to me – shouldn’t you be comparing the way you face now to the way the mouse wants to shoot? But…

Both ifs are triggering. The 1st spins you to face right, then the second checks the changed value of hands, sees you are now facing right, and spins you back left. Replace the 2nd if(…) with a single else to guarantee only one of those happens.

Didn’t work, stumped.,Didnt work, still stumped.

I’m under the impression that Owen is right about this. Firstly, could you place

Debug.Log("less than -0.4, turning.");

and a

Debug.Log("greater than -0.4, turning.";

where appropriate, then test the game and see what the output gives you using the step-by-step feature? It seems to me that both will call during update.

If that is the case, then, as Owen suggested, you should change your code to be like this:

if(hands.transform.rotation.z<-0.4)
       {
         facefor=false;
         me.transform.rotation=new Quaternion(0,0,0,0);
         hands.LookAt(mousepos,Vector3.right);
       }
else
       {
         facefor=true;
         me.transform.rotation=new Quaternion(0,-180,0,0);
         hands.LookAt(mousepos,Vector3.left);
       }

I have used a vector2 and made it to switch directions when the mouse crosses the middle. :slight_smile:

I have used a vector2 and made it to switch directions when the mouse crosses the screens width’s center (screen.width\2). :slight_smile: