#Rotation and flipping

1 messages · Page 1 of 1 (latest)

proven frigate
#

.

carmine moth
#

rename to "quaternions, motherfucker, how do they work!?"

empty nest
#

when I turn to go left, Renderer does Flip()

proven frigate
#

If you're setting the rotation immediately anyway, why not skip storing it as q and just setting it directly onto the transform.rotation?

#

Not that it fixes things

empty nest
proven frigate
#

How many renderers do you have?

#

For the wizard I mean

empty nest
#

Wizard on Renderer with the Wizard Head renderer on LookAT

proven frigate
#

I see what you want now

#

The GameObject "Renderer" rotates right? and you want to flip the head?

dry plover
#

try remove " * Mathf.Rad2Deg;"

#

cuz quaternions use Rad

carmine moth
#

why not just use vector3.LookAt for head?

#

or v2 if it exists

proven frigate
#

If the head should flip I would use SpriteRenderer.flipX

carmine moth
#

oh wait yeah i see the problem

#

🤦‍♂️

#

you need to clamp your rotation

#

transform.rotation += Quaternion.FromToRotation(transform.rotation, vectorToTarget);

should be something like that without clamping

proven frigate
#

I had no idea that such a thing existed

#

That's cool

carmine moth
#

if you use forward axis then you essentially rotate it exactly like euler in 2d
or if it's easier to imagine, vector is like sticking a pen through the object, then rotating the pen

#

i think it's using euler on background but i never checked

empty nest
carmine moth
#

why are you using position

proven frigate
#

So what is it that you're trying to do?

empty nest
carmine moth
#

ah right

empty nest
#

and have an object follow and look at enemy

carmine moth
#

it needs to be transform.forward then

proven frigate
empty nest
#

just = instead of +=?

proven frigate
#

I don't think so

empty nest
carmine moth
#

ahh shit i don't wanna open VS

empty nest
#

otherwise I would just flip sprite renderer

proven frigate
#

So you're basically trying to modify the x value right?

empty nest
#

I flip Y rotation by 180 degrees to essentially flip everything

carmine moth
empty nest
#

then just want to change Z to look at enemy

proven frigate
#

transform.localRotation = Quaternion.Euler(0, transform.localRotation.y + 180, 0);

#

You need to use localRotation if the parent is rotated

#

I think

carmine moth
#

anyways

#

the vector screws it up a little

#

you only need to rotate one side

#

flipping doesn't counter the rotation and that's why it doesn't work

proven frigate
#

What do you mean?

empty nest
#

so if I want my player thats holding a gun to have both flip to opposite side of player when moving right vs left

#

what would you change so I don't have to use 180 degrees Y rotation

proven frigate
#

I don't get why it is a problem?

#

Why not just rotate it 180°?

empty nest
carmine moth
#

then you need to compare position of target, whether it's on the left or right

#

i'm trying to think of solution but it's really counter-intuitive

#

i guess the way i'd do it is dotproduct of where you're facing and just set head to neutral position instead of trying to rotate

#

if you're facing left, then you don't really even care about looking at whatever is behind you(right)

empty nest
#

and if they jump over enemy or enemy them, it needs to swap looking directions

carmine moth
#

hmm

empty nest
#

I think i've got solution though

proven frigate
#

Nice

#

I feel like it should be so simple

carmine moth
#

yeah

empty nest
#

for real

carmine moth
#

really counter-intuitive

#

wouldn't be a problem in 3d

empty nest
#

yep

#

of course 2d doesn't get a nice version

proven frigate
#

I think technically it doesn't matter. For 2D you could just rotate it 90 degrees afterwards

#

I can't see what is going on tho and I am quite confused now lol

empty nest
#

I think you right

carmine moth
#

i can try to explain lol

#

if you are facing left and you're targeting left, everything is fine

#

but if target is on the right, your head flips upside down

#

while the intended result is that if target is on the right, sprite is flipped and rotation is essentially absolute

#

although now that i think of it,

#

since sprite is 2-sided then 3d rotation might've worked too

proven frigate
#

Definitely

carmine moth
#

LookAt with world up?

empty nest
#
public class LookAtRotation : MonoBehaviour
{
    // Update is called once per frame
    [SerializeField] Transform target;
    [SerializeField] Mover parent;
    Quaternion q;
    bool left;

    void Update()
    {
        RotationMethod();
    }
    protected virtual void RotationMethod()
    {
        if (target == null) return;
        //Create a vector to the target
        Vector3 vectorToTarget = target.position - transform.position;
        //Convert this to an angle
        float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
        //Rotate around axis
        left = parent.IsFacingLeft();
        if (left)
        {
            q = Quaternion.Euler(0f, 0f, angle + 180f);
        }
        else
        {
            q = Quaternion.Euler(0f, 0f, angle);

        }
        //Instant rotate
        //transform.rotation += Quaternion.FromToRotation(transform.forward, vectorToTarget);

        transform.localRotation = q;

    }
}
#

finally got it working

#

thanks everyone 🙂

proven frigate
#

Ayyyy

#

I'll close the thread then

empty nest
#

wait

#

Ima see if I can't use the LookAt 3d method easy 😄

proven frigate
#

Okay

empty nest
# proven frigate Okay

if you could store the first transform and not exectute to do something like this I think it would work

#

but as is I think 3d lookat for 2d just sucks lol

proven frigate
#

I would take the transform.LookAt(target) and rotate 90° around the local y-axis

empty nest
#

the X rotation is the value you should use for Z funny enough lol

proven frigate
#

Hence the "around the local y-axis"

empty nest
#

ahhh

#

yeah I have no clue what you are talking about then 😄

proven frigate
#

endRot = Quaternion.AngleAxis(90, transform.up) * transform.localRotation.eulerAngles;

#

Something like that

#

To rotate it

#

endRot is a Vector3

#

So the end product would be something like:

transform.LookAt(target);
transform.localRotation = Quaternion.Euler(Quaternion.AngleAxis(90, transform.up) * transform.localRotation.eulerAngles);```
carmine moth
#

lookat already rotates, why are you rotating it the second time?

proven frigate
#

So it is invisible

#

You have to rotate it 90°

#

Since forward is into the screen

#

And LookAt() sets the forward towards the target

carmine moth
#

oh right, forward is z in 3d but x in 2d

proven frigate
#

Wait no

carmine moth
#

2nd rotation is a weird workaround though

proven frigate
#

It's easy and pretty fast

carmine moth
#

easy and fast is the best way to make bad code

proven frigate
#

Tell me a single reason why it is a "weird workaround"

carmine moth
#

is it not?

proven frigate
#

Why would it be?

carmine moth
#

you're performing 2 similar operations instead of 1

#

it's fine with 1 character but it's something done in update and potentially dozens of enemies

proven frigate
#

The alternative is to do the LookAt manually which makes reading it wayy harder

carmine moth
#

might be too tiny of an optimization but it still kinda sucks

proven frigate
#

It's not worth remaking the LookAt method

carmine moth
#

either way it's fine

proven frigate
#

Which is the alternative

carmine moth
#

all that really matters is that the abstraction is there

proven frigate
carmine moth
#

void HeadLooksAtTarget(target)

the rest can be rewritten later

proven frigate
#

I don't get why you would spend a lot of time fixing something that means next to nothing. If you keep doing that, you will be stuck in the project forever without getting anywhere

#

That's the reason to 50% of my failed projects

carmine moth
#

does that mean you finished first project and got stuck forever in 2nd?

proven frigate
#

No, I lost interest in the project becasue I got nowhere

carmine moth
#

that's why i don't like goal-oriented people

#

if you do one method right, you can reuse it later

proven frigate
#

These are a few of my projects. Many of them got lost because I lost motivation

carmine moth
#

that's actually not the case

#

you lack discipline

proven frigate
#

Nah

#

You know nothing about me

#

I have made probably more than 100 projects with the sole purpose of learning a single thing

carmine moth
#

i can tell you how i don't lose motivation on projects

#

i just lose motivation on boring parts of the projects, instead of the entire project

#

that means i can still return back to it later

proven frigate
#

Like A* Pathfinding, or boids, or chess AI, or evolution simulation, ray marchin, etc.

carmine moth
#

but the project will progress

proven frigate
#

That's cool

#

If I get nowhere I will lose all motivation. As long as I am going at a steady pace everything is good

#

Then I clean up at the end

#

It's not a bad thing to make code that can be used for future projects though

#

I have used the A* pathfinding library I made lots of times, fx. in my latest prject where I made a hexagonal grid. I am sure that there is a "proper" way to make a hexagon grid, but the solution I came up with I can use for any project that requires such a grid

#

And the A* is versatile to work with any grid

#

Stuff like that I optimize

#

Other stuff that is specific to the project I don't really care about

#

Waste of time

carmine moth
#

i don't like leaving something half-assed because i might end up reusing it later, and seeing it again will destroy motivation and the momentum since i'll probably start reworking that instead