Using transform.eulerAngles Correctly

This is my first time asking any questions here, so I might be missing something really obvious. I’m generally new to writing scripts, and got caught up with the fact that in one of my scripts, transform.eulerAngles.y came up as a number, while in the current script I am writing, transform.eulerAngles.y seems to be coming up as a boolean. Again, I’m probably missing something very obvious or am making a beginner’s mistake. My script should be attached if the code doesn’t come up properly in my post. Hopefully, someone can aid me.

var anim : Animator;

var speed : float = 10.0;
var rotationSpeed : float = 100.0;
var move = transform.eulerAngles.y;

function Update (){

    if(Input.GetButton("Vertical")){

        var horizontal : float;
        var vertical : float;

        if (0 > move > 90){
           horizontal = 1 - (move / 90);
           vertical = (move / 90);
        }

        if (move == 0){
            horizontal = 1;
            vertical = 0;
        }

        if (move == 90){
            horizontal = 0;
            vertical = 1;
        }

        if (move == 180){
            horizontal = 1;
            vertical = 0;
        }

        if (move == 270){
            horizontal = 0;
            vertical = -1;
        }

        if (91 > move > 180){
            horizontal = -1 + (move / 180);
            vertical = (180 / move) - 1;
        }

        if (181 > move > 270){
            horizontal = -1 + (move / 270);
            vertical = -1 * (270 / move);
        }

        if (271 > move > 360){
            vertical = 1 - (move / 360);
            horizontal = (move / 360);
        }


        transform.Translate(horizontal, 0, vertical);
    }

}

2661886–187685–PyroController.js (1.13 KB)

So I kind of realized that I seemed to be unable to place x > move > y in an if statement, so I changed that and things seem to be working better. Sorry to waste anyone’s time; it was a simple mistake.

The best way to write this is

(x < 250 && x > 100)

Unfortunately the nice intuitive mathematical notation of a < x < b doesn’t actually work in C#

If you are coming from a mathematical background its also worth watching for = and ==. They frequently catch me out.

I can see some major issues with your code though.

  1. Values from 90->91, 180->181, 270->271 will be completely ignored (resulting in 0 for both hor and vert).

  2. ‘move’ is a float, you’ll seldom if ever get an exact 0,90,180,270… those == will most likely never occur, as you’ll probably get values like 90.0001. And because of the (1) thing I already mentioned, you’ve got more dead zones.

  3. It’s bad to assume rotational range being 0->360. Though I think transform.eulerAngles is in that range, it’s a habit you don’t want to go down the route of, what if you use this somewhere where instead of 270, you get -90, both of which are the same orientation, yet different numerically.

  4. Your values of 0,1 or 1,0, or 0,-1, and the sort when you == 90, 180, 270… implies that you expect your vertical values to occelate between -1->1 linearly, and your horizontal values to occelate from 0->1.
    But when you have this:
    horizontal = -1 + (move / 270)

The problem is ‘move’ will only ever be between 181 and 270, which means your result will be in the range -0.66->0

You could get this more easily with arithmetic only.

var horizontal:float = Mathf.PingPong(move + 90f, 90f) / 90f;
var vertical:float = (Mathf.PingPong(move + 90f, 180f) - 90f) / 90f;

That looks like it could be what you’re really looking for.

I may be mistaken though…