- Home /
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);
}
Answer by benk0913 · Sep 10, 2012 at 08:26 AM
I have used a vector2 and made it to switch directions when the mouse crosses the middle. :)
Answer by Owen-Reynolds · Sep 08, 2012 at 03:14 PM
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.
is there a cool program$$anonymous$$g term of art (along the lines of, say, "unity error" or "infinite loop" or the like) when you fogget that you have already changed a value in an algorithm ?!!
Answer by kristercollin · Sep 09, 2012 at 05:00 PM
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);
}
Tried so, didnt work. I tested it before I asked the question ):