#Player snaps to sides when is A and D clicked

1 messages ยท Page 1 of 1 (latest)

cerulean charm
#

I want to make some movement but my Player always snaps and i dont know why

oak ivy
#

Send a video of what happens and then code

cerulean charm
#

kk

#

So what you think?

oak ivy
#

Can you show the player sprite transform

#

Is it 0, 0, 0

#

The sprite is parented I assume?

#

@cerulean charm

#

What happens is probably you flipping the axis on a parent. But child objects arent centered to the parent. So when they flip they move

#

A way around this would be to flip the sprite directly using SpriteRender.flip

#

Or to make sure the children are aligned correctly

cerulean charm
oak ivy
#

its like the image

#

your player is like a 2D image right

cerulean charm
#

yes

oak ivy
#

its parented?

#

to something?

cerulean charm
#

i dont think so

oak ivy
#

In the hierarchy can you show the object

cerulean charm
#

when i click a or d it will set ScaleX to - or +

oak ivy
#

yeah so which of these child objects have a sprite

cerulean charm
#

i think all of them

oak ivy
#

you need to make sure children transforms scale properly or you can use SpriteRenderer Flip

#

Also why do you set local scale again?

cerulean charm
#

how i can do that?

#

sorry but can you try explain what is local scale? blobfacepalm

#

i am little bit dumb

oak ivy
#

its like the scale relative to the parent

#

I can show you an example, sec

#

lemme open unity

cerulean charm
#

kk

oak ivy
#

Btw I recommend u create a new scene atm

#

so you can learn about local scales and parent scales

#

create a new scene with an empty object, create 5 cubes and child them to the empty object

cerulean charm
#

like that?

oak ivy
#

Yea

#

So

#

Create a line with those squares

#

in any axis you like

#

make Square's position 1 , 0, 0 for example
make Square2's postion 2, 0, 0 etc

cerulean charm
#

ok

#

done

oak ivy
#

send a quick ss

#

Of what it looks like

cerulean charm
oak ivy
#

okay not bad

#

Uh so now move the parent

#

When you move the parent, you'll notice all the cubes

#

move relative to the parent

cerulean charm
#

i see

oak ivy
#

So because its parented

#

any changes to transform to parent will reflect on children

cerulean charm
#

aaaaa allright i got it ๐Ÿ˜„

#

nice

oak ivy
#

so what I think your issue was

#

Delete all 3 cubes

#

leave only one

#

and move it away from the parent

#

if you flip the parent, it will appear on the other side

#

because the child isn't centered to the parent

#

the circle is the parent here, cube is child

#

Local Scale: 1

#

The circle flips

#

but the cube will move relative to that

#

do you get what I mean?

cerulean charm
#

little bit

oak ivy
#

You can replicate it

#

Put the parent, the put a child object to the left of the parent

#

Now set the parent's transform scale on X to 1

#

Then set it to -1

#

Thats what you're doing in your code

cerulean charm
#

oh ok i see

#

but i dont know how can i change it

#

oh wait a second please

oak ivy
#

tyt

cerulean charm
#

i dont know what to do now.

oak ivy
#

Can you show the transform on ninja gameobject?

cerulean charm
#

yes

oak ivy
#

Set position to 0, 0, 0

#

The position you see in the inspector is relative to parent

#

it's not world transform

cerulean charm
#

he is gone

oak ivy
#

Oh okay change only the X

#

to 0

#

It's probably because the Z axis is now equal or greater than camera

cerulean charm
#

gone too

oak ivy
#

point is make the ninja the center of the parent

#

Im not sure whats happening there, but you can just do it manually

#

See those 2 red dots?

#

1st belongs to Player Gameobject

#

that's the center of Player, move the ninja to be closer to the center

cerulean charm
#

ou wait XDD

#

i was changing the Scale

oak ivy
#

oh... lmfao

cerulean charm
#

ok done 0.0.0

oak ivy
#

okay test

cerulean charm
#

same

oak ivy
#

What are you flipping, ninja or the parent?

cerulean charm
#

Player

#

parent

oak ivy
#

can you show me what happens in video when you move?

#

is it the same thing as before?

cerulean charm
#

yes it goes negative and positive

#

scale

oak ivy
#

sec

#

The first behavior is when the center is not aligned with the parent

#

But if you're struggling to align it you can just use spriterenderer flip

cerulean charm
#

how can i do that?

oak ivy
#

You need to get the SpriteRender component

#

and change the property

cerulean charm
cerulean charm
oak ivy
#

Show in code where you get it

cerulean charm
#

sorry but i am confused

oak ivy
#

You can try flipping something simpler

#

Create a 2D circle

#

then create a script

#

Get the component of the sprite renderer

#

Then change the property

cerulean charm
#

i fliped X and ninja doesnt moves

oak ivy
#

okay then it works?

#

it doesn't do the weird movement anymore?

#

or what do you mean

cerulean charm
#

it doesnt move like doesnt move it just stays where it is ๐Ÿ˜„

oak ivy
#

So it doesn't flip either?

#

or it does?

cerulean charm
#

i think i should delete this and start from scratch

oak ivy
#

I think you're trying to make something without understanding the environment

#

I would say play around with Unity and understand how it works

#

before making a game

cerulean charm
#

Okay, Icetrack, thanks for your time. I really appreciate it

oak ivy
#

no worries

#

If you wanna understand like more about your issue

#

to replicate it just copy your previous code with flip

#

Put the parent object in the middle

#

and put the child object to the left

#

when you flip the parent object, the child object will be at the right

#

but if the parent and child object are on the same coordinates

#

Then it doesn't move when flipped

cerulean charm
#

yeah yeak okay ๐Ÿ™‚

#

Thx ๐Ÿ˜„