#Conditions

1 messages · Page 1 of 1 (latest)

vital sun
#

The first piece is how do you say "OR" in C#?

#

that would be ||

pallid bone
#

thanks

#

i didnt know that

vital sun
#

the next piece is that you have to be a bit more verbose

#

in Python, you can do something like -90 < rotation < 90, but that doesn't fly in C#

#

you have to be much more explicit

pallid bone
#

dang, alright

vital sun
#

I've also just realized a subtle mistep on my part

#

you're looking for AND, which is &&

#

(that one's a bit easier to remember than or, haha)

pallid bone
vital sun
#

in this case, something like this:

if (transform.rotation.z > -90 && transform.rotation.z < 90)
#

you may want >= and <= respectively if you want it to still happen when it's exactly 90 or -90

pallid bone
#

alright

vital sun
#

a trick that'll help you get where you need, if you don't already know it, is to put Debug.Log(something) in various places to figure out what is happening, what isn't happening, and what things are at those times

pallid bone
#

so something like this?

if(transform.parent.rotation.z <= -90 && transform.parent.rotation.z >= 90) { 
    GetComponent<SpriteRenderer>().flipX = true; 
}```
vital sun
#

possibly!

pallid bone
#

ima test it

vital sun
#

that looks right-ish

#

if the flipX thing doesn't work, you might need to do transform.localScale

pallid bone
#

yeah, nothing happened

#

ill try the transform.localScale

vital sun
#

alrighty

#

mine looks like this for a 2d platformer that flips based on player input:

if (playerInputHorizontalDirection > 0)
    {
      transform.localScale = Vector3.one;
    }
    else if (playerInputHorizontalDirection < 0)
    {
      transform.localScale = new Vector3(-1, 1, 1);
    }
pallid bone
#

so something like this would work?

void Update() {
    if(transform.parent.rotation.z <= -90 && transform.parent.rotation.z >= 90) { 
        transform.localScale = new Vector3 (-1, 1, 1);
    }
}```
#

hmm, it still seems to be doing nothing

vital sun
#

Possibly

#

I'd debug log transform and transform parent

#

See what they are

pallid bone
#

it doesnt seem to be logging anything

#

wait, my bad, i put the debug log in the if statement

#

alright i put this in the update method

Debug.Log(transform, transform.parent);
#

and this is what it logged

vital sun
#

Oh hmmm. Try logging transform.rotation

#

When in doubt log whatever she see where you're at

pallid bone
#

alright, i put this into the update method

Debug.Log(transform.rotation);
#

it logged this

#

i also had it log the transform.parent and it logged this

pallid bone
vital sun
#

Ok so not parent

pallid bone
#

the weapon parent is what rotates

vital sun
#

Right, but the rotation is only showing up when you do transform.rotation

pallid bone
#

alright

vital sun
#

But maybe its rotating is separate of its parent

pallid bone
#

yeah, the rotation of the sprite doesnt change, thats why i was originally trying to flip the sprite from the parent instead of the sprite

#

so something like:

void Update() {
    if(transform.rotation.z <= -90 && transform.rotation.z >= 90) { 
        GetComponentInChildren<SpriteRenderer>.flipX = true;
    }
}```
#

That didnt work, and i think its because it isnt getting the rotation or something, because when i log something in the if statement it never logs anything no matter what the rotation is.

#

but i dont understand why

vital sun
#

Oh I think it might be in radians not degrees

pallid bone
#

oh

#

okay

#

whats that mean

vital sun
#

Radians vs degrees

#

Different ways of representing angles

pallid bone
#

wait that actually makes sense, because the range for the angle jumps from 180 to -180 on the rotation

vital sun
#

I don't know the c sharp stuff for it but there's a library called Mathf that should provide the necessary conversion functions

#

I'm stepping away for a bit though just a heads up